Skip to content

Commit

Permalink
feat(cli)!: set log level to DEBUG on --verbose if RUSTUP_LOG i…
Browse files Browse the repository at this point in the history
…s unset
  • Loading branch information
rami3l committed Aug 10, 2024
1 parent 827bcb7 commit 48412d4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/bin/rustup-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn run_rustup_inner(
utils::current_exe()?;

match process.name().as_deref() {
Some("rustup") => rustup_mode::main(current_dir, process).await,
Some("rustup") => rustup_mode::main(current_dir, process, Some(console_filter)).await,
Some(n) if n.starts_with("rustup-setup") || n.starts_with("rustup-init") => {
// NB: The above check is only for the prefix of the file
// name. Browsers rename duplicates to
Expand Down
30 changes: 25 additions & 5 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use clap::{builder::PossibleValue, Args, CommandFactory, Parser, Subcommand, Val
use clap_complete::Shell;
use itertools::Itertools;
use tracing::{info, trace, warn};
use tracing_subscriber::{reload::Handle, EnvFilter, Registry};

use crate::{
cli::{
Expand Down Expand Up @@ -68,12 +69,12 @@ fn handle_epipe(res: Result<utils::ExitCode>) -> Result<utils::ExitCode> {
after_help = RUSTUP_HELP,
)]
struct Rustup {
/// Enable verbose output
#[arg(short, long)]
/// Enable verbose output, limit console logger level to 'DEBUG' if 'RUSTUP_LOG' is unset
#[arg(short, long, overrides_with = "quiet")]
verbose: bool,

/// Disable progress output
#[arg(short, long, conflicts_with = "verbose")]
/// Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
#[arg(short, long, overrides_with = "verbose")]
quiet: bool,

/// Release channel (e.g. +stable) or custom toolchain to set override
Expand Down Expand Up @@ -532,7 +533,11 @@ enum SetSubcmd {
}

#[tracing::instrument(level = "trace", fields(args = format!("{:?}", process.args_os().collect::<Vec<_>>())))]
pub async fn main(current_dir: PathBuf, process: &Process) -> Result<utils::ExitCode> {
pub async fn main(
current_dir: PathBuf,
process: &Process,
console_filter: Option<Handle<EnvFilter, Registry>>,
) -> Result<utils::ExitCode> {
self_update::cleanup_self_updater(process)?;

use clap::error::ErrorKind::*;
Expand Down Expand Up @@ -570,6 +575,21 @@ pub async fn main(current_dir: PathBuf, process: &Process) -> Result<utils::Exit
}
};

if process.var("RUSTUP_LOG").is_err() {
if let Some(console_filter) = &console_filter {
if matches.quiet {
console_filter
.modify(|it| *it = EnvFilter::new("rustup=WARN"))
.expect("error reloading `EnvFilter` for console_logger")
}
if matches.verbose {
console_filter
.modify(|it| *it = EnvFilter::new("rustup=DEBUG"))
.expect("error reloading `EnvFilter` for console_logger")
}
}
}

let cfg = &mut common::set_globals(current_dir, matches.quiet, process)?;

if let Some(t) = &matches.plus_toolchain {
Expand Down
21 changes: 14 additions & 7 deletions src/cli/setup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use crate::{
before_help = format!("rustup-init {}", common::version()),
)]
struct RustupInit {
/// Enable verbose output
#[arg(short, long)]
/// Enable verbose output, limit console logger level to 'DEBUG' if 'RUSTUP_LOG' is unset
#[arg(short, long, overrides_with = "quiet")]
verbose: bool,

/// Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
#[arg(short, long)]
#[arg(short, long, overrides_with = "verbose")]
quiet: bool,

/// Disable confirmation prompt
Expand Down Expand Up @@ -115,10 +115,17 @@ pub async fn main(
warn!("{}", common::WARN_COMPLETE_PROFILE);
}

if quiet && process.var("RUSTUP_LOG").is_err() {
console_filter
.modify(|it| *it = EnvFilter::new("rustup=WARN"))
.expect("error reloading `EnvFilter` for console_logger");
if process.var("RUSTUP_LOG").is_err() {
if quiet {
console_filter
.modify(|it| *it = EnvFilter::new("rustup=WARN"))
.expect("error reloading `EnvFilter` for console_logger");
}
if verbose {
console_filter
.modify(|it| *it = EnvFilter::new("rustup=DEBUG"))
.expect("error reloading `EnvFilter` for console_logger");
}
}

let opts = InstallOpts {
Expand Down
3 changes: 2 additions & 1 deletion src/test/mock/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ impl Config {
}

let tp = process::TestProcess::new(&*self.workdir.borrow(), &arg_strings, vars, "");
let process_res = rustup_mode::main(tp.process.current_dir().unwrap(), &tp.process).await;
let process_res =
rustup_mode::main(tp.process.current_dir().unwrap(), &tp.process, None).await;
// convert Err's into an ec
let ec = match process_res {
Ok(process_res) => process_res,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Usage: rustup-init[EXE] [OPTIONS]
Options:
-v, --verbose
Enable verbose output
Enable verbose output, limit console logger level to 'DEBUG' if 'RUSTUP_LOG' is unset
-q, --quiet
Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
-y
Expand Down
6 changes: 4 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ Arguments:
[+toolchain] Release channel (e.g. +stable) or custom toolchain to set override
Options:
-v, --verbose Enable verbose output
-q, --quiet Disable progress output
-v, --verbose Enable verbose output, limit console logger level to 'DEBUG' if 'RUSTUP_LOG' is
unset
-q, --quiet Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is
unset
-h, --help Print help
-V, --version Print version
Expand Down
6 changes: 4 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ Arguments:
[+toolchain] Release channel (e.g. +stable) or custom toolchain to set override
Options:
-v, --verbose Enable verbose output
-q, --quiet Disable progress output
-v, --verbose Enable verbose output, limit console logger level to 'DEBUG' if 'RUSTUP_LOG' is
unset
-q, --quiet Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is
unset
-h, --help Print help
-V, --version Print version
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ Arguments:
Options:
-v, --verbose
Enable verbose output
Enable verbose output, limit console logger level to 'DEBUG' if 'RUSTUP_LOG' is unset
-q, --quiet
Disable progress output
Disable progress output, limit console logger level to 'WARN' if 'RUSTUP_LOG' is unset
-h, --help
Print help
Expand Down

0 comments on commit 48412d4

Please sign in to comment.