Skip to content

Commit

Permalink
Merge pull request rust-lang#281 from cargo-bins/fix-quiet
Browse files Browse the repository at this point in the history
Improve doc of cmdline option `log_level`, add `--quiet` and pass it to `cargo-install`
  • Loading branch information
NobodyXu authored Aug 5, 2022
2 parents 64e948f + c352eb0 commit b46e54b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/binstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ pub struct Options {
pub manifest_path: Option<PathBuf>,
pub cli_overrides: PkgOverride,
pub desired_targets: DesiredTargets,
pub quiet: bool,
}
20 changes: 14 additions & 6 deletions src/binstall/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub async fn install(
.await
.map(|option| {
option.map(|bins| MetaData {
name: name.into(),
version_req: version.into(),
name,
version_req: version,
current_version,
source: Source::cratesio_registry(),
target,
Expand All @@ -47,7 +47,7 @@ pub async fn install(
.ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?;

if !opts.dry_run {
install_from_source(package, target, jobserver_client)
install_from_source(package, target, jobserver_client, opts.quiet)
.await
.map(|_| None)
} else {
Expand Down Expand Up @@ -126,6 +126,7 @@ async fn install_from_source(
package: Package<Meta>,
target: &str,
lazy_jobserver_client: LazyJobserverClient,
quiet: bool,
) -> Result<()> {
let jobserver_client = lazy_jobserver_client.get().await?;

Expand All @@ -136,13 +137,20 @@ async fn install_from_source(
let mut command = process::Command::new("cargo");
jobserver_client.configure(&mut command);

let mut child = Command::from(command)
.arg("install")
let mut cmd = Command::from(command);

cmd.arg("install")
.arg(package.name)
.arg("--version")
.arg(package.version)
.arg("--target")
.arg(&*target)
.arg(&*target);

if quiet {
cmd.arg("quiet");
}

let mut child = cmd
.spawn()
.into_diagnostic()
.wrap_err("Spawning cargo install failed.")?;
Expand Down
38 changes: 36 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
time::{Duration, Instant},
};

use clap::{AppSettings, Parser};
use clap::{builder::PossibleValue, AppSettings, Parser};
use compact_str::CompactString;
use log::{debug, error, info, warn, LevelFilter};
use miette::{miette, Result, WrapErr};
Expand Down Expand Up @@ -148,14 +148,44 @@ struct Options {

/// Utility log level
///
/// Set to `trace` to print very low priority, often extremely
/// verbose information.
///
/// Set to `debug` when submitting a bug report.
///
/// Set to `info` to only print useful information.
///
/// Set to `warn` to only print on hazardous situations.
///
/// Set to `error` to only print serious errors.
///
/// Set to `off` to disable logging completely, this will also
/// disable output from `cargo-install`.
#[clap(
help_heading = "Meta",
long,
default_value = "info",
value_name = "LEVEL"
value_name = "LEVEL",
possible_values = [
PossibleValue::new("trace").help(
"Set to `trace` to print very low priority, often extremely verbose information."
),
PossibleValue::new("debug").help("Set to debug when submitting a bug report."),
PossibleValue::new("info").help("Set to info to only print useful information."),
PossibleValue::new("warn").help("Set to warn to only print on hazardous situations."),
PossibleValue::new("error").help("Set to error to only print serious errors."),
PossibleValue::new("off").help(
"Set to off to disable logging completely, this will also disable output from `cargo-install`."
),
]
)]
log_level: LevelFilter,

/// Equivalent to setting `log_level` to `off`.
///
/// This would override the `log_level`.
#[clap(help_heading = "Meta", short, long)]
quiet: bool,
}

enum MainExit {
Expand Down Expand Up @@ -229,6 +259,9 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {

// Load options
let mut opts = Options::parse_from(args);
if opts.quiet {
opts.log_level = LevelFilter::Off;
}

let crate_names = take(&mut opts.crate_names);
if crate_names.len() > 1 {
Expand Down Expand Up @@ -320,6 +353,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
manifest_path: opts.manifest_path.take(),
cli_overrides,
desired_targets,
quiet: opts.log_level == LevelFilter::Off,
});

let tasks: Vec<_> = if !opts.dry_run && !opts.no_confirm {
Expand Down

0 comments on commit b46e54b

Please sign in to comment.