Skip to content

Commit

Permalink
feat(inspect): add inspect command to view build script
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Nov 3, 2024
1 parent b2fc746 commit bcef36c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ cargo install --path .
Usage: soar [OPTIONS] <COMMAND>
Commands:
install Install packages [aliases: i]
search Search package [aliases: s]
install Install packages [aliases: i, add]
search Search package [aliases: s, find]
query Query package info [aliases: Q]
remove Remove packages [aliases: r]
sync Sync with remote metadata [aliases: S]
update Update packages [aliases: u]
info Show info about installed packages
list List all available packages
inspect Inspect package build log
run Run packages without installing to PATH [aliases: exec]
remove Remove packages [aliases: r, del]
sync Sync with remote metadata [aliases: S, fetch]
update Update packages [aliases: u, upgrade]
info Show info about installed packages [aliases: list-installed]
list List all available packages [aliases: ls]
log Inspect package build log
inspect Inspect package build script
run Run packages without installing to PATH [aliases: exec, execute]
use Use package from different family
download Download arbitrary files [aliases: dl]
health Health check
Expand Down
27 changes: 18 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Args {
pub enum Commands {
/// Install packages
#[command(arg_required_else_help = true)]
#[clap(name = "install", visible_alias = "i", alias = "add")]
#[clap(name = "install", visible_alias = "i", visible_alias = "add")]
Install {
/// Packages to install
#[arg(required = true)]
Expand Down Expand Up @@ -54,7 +54,7 @@ pub enum Commands {

/// Search package
#[command(arg_required_else_help = true)]
#[clap(name = "search", visible_alias = "s", alias = "find")]
#[clap(name = "search", visible_alias = "s", visible_alias = "find")]
Search {
/// Query to search
#[arg(required = true)]
Expand All @@ -76,7 +76,7 @@ pub enum Commands {

/// Remove packages
#[command(arg_required_else_help = true)]
#[clap(name = "remove", visible_alias = "r", alias = "del")]
#[clap(name = "remove", visible_alias = "r", visible_alias = "del")]
Remove {
/// Packages to remove
#[arg(required = true)]
Expand All @@ -88,27 +88,27 @@ pub enum Commands {
},

/// Sync with remote metadata
#[clap(name = "sync", visible_alias = "S", alias = "fetch")]
#[clap(name = "sync", visible_alias = "S", visible_alias = "fetch")]
Sync,

/// Update packages
#[clap(name = "update", visible_alias = "u", alias = "upgrade")]
#[clap(name = "update", visible_alias = "u", visible_alias = "upgrade")]
Update {
/// Packages to update
#[arg(required = false)]
packages: Option<Vec<String>>,
},

/// Show info about installed packages
#[clap(name = "info", alias = "list-installed")]
#[clap(name = "info", visible_alias = "list-installed")]
ListInstalledPackages {
/// Packages to get info about
#[arg(required = false)]
packages: Option<Vec<String>>,
},

/// List all available packages
#[clap(name = "list", alias = "ls")]
#[clap(name = "list", visible_alias = "ls")]
ListPackages {
/// Which collection to get the packages from
#[arg(required = false)]
Expand All @@ -118,15 +118,24 @@ pub enum Commands {
/// Inspect package build log
#[command(arg_required_else_help = true)]
#[clap(name = "log")]
Inspect {
Log {
/// Package to view log for
#[arg(required = true)]
package: String,
},

/// Inspect package build script
#[command(arg_required_else_help = true)]
#[clap(name = "inspect")]
Inspect {
/// Package to view build script for
#[arg(required = true)]
package: String,
},

/// Run packages without installing to PATH
#[command(arg_required_else_help = true)]
#[clap(name = "run", visible_alias = "exec", alias = "execute")]
#[clap(name = "run", visible_alias = "exec", visible_alias = "execute")]
Run {
/// Skip all prompts and use first
#[arg(required = false, short, long)]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ pub async fn init() -> Result<()> {
registry.list(collection.as_deref()).await?;
}
Commands::Inspect { package } => {
registry.inspect(&package).await?;
registry.inspect(&package, "script").await?;
}
Commands::Log { package } => {
registry.inspect(&package, "log").await?;
}
Commands::Run { command, yes } => {
registry.run(command.as_ref(), yes).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ impl PackageRegistry {
Ok(())
}

pub async fn inspect(&self, package_name: &str) -> Result<()> {
self.storage.inspect(package_name).await
pub async fn inspect(&self, package_name: &str, inspect_type: &str) -> Result<()> {
self.storage.inspect(package_name, inspect_type).await
}

pub async fn run(&self, command: &[String], yes: bool) -> Result<()> {
Expand Down
31 changes: 24 additions & 7 deletions src/registry/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,31 @@ impl PackageStorage {
.collect()
}

pub async fn inspect(&self, package_name: &str) -> Result<()> {
pub async fn inspect(&self, package_name: &str, inspect_type: &str) -> Result<()> {
let resolved_pkg = self.resolve_package(package_name, false)?;

let client = reqwest::Client::new();
let url = resolved_pkg.package.build_log;
let url = if inspect_type == "log" {
resolved_pkg.package.build_log
} else if resolved_pkg
.package
.build_script
.starts_with("https://github.com")
{
resolved_pkg
.package
.build_script
.replace("tree", "raw/refs/heads")
} else {
resolved_pkg.package.build_script
};

let response = client.get(&url).send().await?;

if !response.status().is_success() {
return Err(anyhow::anyhow!(
"Error fetching log from {} [{}]",
"Error fetching build {} from {} [{}]",
inspect_type,
url.color(Color::Blue),
response.status().color(Color::Red)
));
Expand All @@ -403,7 +418,8 @@ impl PackageStorage {
let content_length = response.content_length().unwrap_or_default();
if content_length > 1_048_576 {
warn!(
"The log file is too large ({}). Do you really want to download and view it (y/N)? ",
"The build {} file is too large ({}). Do you really want to download and view it (y/N)? ",
inspect_type,
format_bytes(content_length).color(Color::Magenta)
);

Expand All @@ -418,7 +434,8 @@ impl PackageStorage {
}

println!(
"Fetching log from {} [{}]",
"Fetching {} from {} [{}]",
inspect_type,
url.color(Color::Blue),
format_bytes(response.content_length().unwrap_or_default()).color(Color::Magenta)
);
Expand All @@ -430,9 +447,9 @@ impl PackageStorage {
let chunk = chunk.context("Failed to read chunk")?;
content.extend_from_slice(&chunk);
}
let log_str = String::from_utf8_lossy(&content).replace("\r", "\n");
let output = String::from_utf8_lossy(&content).replace("\r", "\n");

println!("\n{}", log_str);
println!("\n{}", output);

Ok(())
}
Expand Down

0 comments on commit bcef36c

Please sign in to comment.