Merge pull request #281 from cargo-bins/fix-quiet

Improve doc of cmdline option `log_level`, add `--quiet` and pass it to `cargo-install`
This commit is contained in:
Jiahao XU 2022-08-05 15:35:56 +10:00 committed by GitHub
commit b46e54b53a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 8 deletions

View file

@ -17,4 +17,5 @@ pub struct Options {
pub manifest_path: Option<PathBuf>, pub manifest_path: Option<PathBuf>,
pub cli_overrides: PkgOverride, pub cli_overrides: PkgOverride,
pub desired_targets: DesiredTargets, pub desired_targets: DesiredTargets,
pub quiet: bool,
} }

View file

@ -30,8 +30,8 @@ pub async fn install(
.await .await
.map(|option| { .map(|option| {
option.map(|bins| MetaData { option.map(|bins| MetaData {
name: name.into(), name,
version_req: version.into(), version_req: version,
current_version, current_version,
source: Source::cratesio_registry(), source: Source::cratesio_registry(),
target, target,
@ -47,7 +47,7 @@ pub async fn install(
.ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?; .ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?;
if !opts.dry_run { if !opts.dry_run {
install_from_source(package, target, jobserver_client) install_from_source(package, target, jobserver_client, opts.quiet)
.await .await
.map(|_| None) .map(|_| None)
} else { } else {
@ -126,6 +126,7 @@ async fn install_from_source(
package: Package<Meta>, package: Package<Meta>,
target: &str, target: &str,
lazy_jobserver_client: LazyJobserverClient, lazy_jobserver_client: LazyJobserverClient,
quiet: bool,
) -> Result<()> { ) -> Result<()> {
let jobserver_client = lazy_jobserver_client.get().await?; let jobserver_client = lazy_jobserver_client.get().await?;
@ -136,13 +137,20 @@ async fn install_from_source(
let mut command = process::Command::new("cargo"); let mut command = process::Command::new("cargo");
jobserver_client.configure(&mut command); jobserver_client.configure(&mut command);
let mut child = Command::from(command) let mut cmd = Command::from(command);
.arg("install")
cmd.arg("install")
.arg(package.name) .arg(package.name)
.arg("--version") .arg("--version")
.arg(package.version) .arg(package.version)
.arg("--target") .arg("--target")
.arg(&*target) .arg(&*target);
if quiet {
cmd.arg("quiet");
}
let mut child = cmd
.spawn() .spawn()
.into_diagnostic() .into_diagnostic()
.wrap_err("Spawning cargo install failed.")?; .wrap_err("Spawning cargo install failed.")?;

View file

@ -8,7 +8,7 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use clap::{AppSettings, Parser}; use clap::{builder::PossibleValue, AppSettings, Parser};
use compact_str::CompactString; use compact_str::CompactString;
use log::{debug, error, info, warn, LevelFilter}; use log::{debug, error, info, warn, LevelFilter};
use miette::{miette, Result, WrapErr}; use miette::{miette, Result, WrapErr};
@ -148,14 +148,44 @@ struct Options {
/// Utility log level /// 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 `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( #[clap(
help_heading = "Meta", help_heading = "Meta",
long, long,
default_value = "info", 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, 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 { enum MainExit {
@ -229,6 +259,9 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
// Load options // Load options
let mut opts = Options::parse_from(args); let mut opts = Options::parse_from(args);
if opts.quiet {
opts.log_level = LevelFilter::Off;
}
let crate_names = take(&mut opts.crate_names); let crate_names = take(&mut opts.crate_names);
if crate_names.len() > 1 { if crate_names.len() > 1 {
@ -320,6 +353,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
manifest_path: opts.manifest_path.take(), manifest_path: opts.manifest_path.take(),
cli_overrides, cli_overrides,
desired_targets, desired_targets,
quiet: opts.log_level == LevelFilter::Off,
}); });
let tasks: Vec<_> = if !opts.dry_run && !opts.no_confirm { let tasks: Vec<_> = if !opts.dry_run && !opts.no_confirm {