From d87ee9a1703c9cd5be25bcb1a0d33620308eb99f Mon Sep 17 00:00:00 2001 From: rami3l Date: Wed, 5 Jun 2024 19:53:23 +0800 Subject: [PATCH 1/2] feat(cli): add `--quiet` to `rustup toolchain list` --- src/cli/common.rs | 9 ++++++++- src/cli/rustup_mode.rs | 8 ++++++-- ...up_toolchain_cmd_list_cmd_help_flag_stdout.toml | 1 + tests/suite/cli_rustup.rs | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/cli/common.rs b/src/cli/common.rs index 45eae5760a..22ab30e446 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -397,7 +397,7 @@ pub(super) fn list_items( Ok(utils::ExitCode(0)) } -pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result { +pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool, quiet: bool) -> Result { let toolchains = cfg.list_toolchains()?; if toolchains.is_empty() { writeln!(process().stdout().lock(), "no installed toolchains")?; @@ -422,6 +422,7 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result Result Result<()> { + if quiet { + writeln!(process().stdout().lock(), "{toolchain}")?; + return Ok(()); + } + let toolchain_path = cfg.toolchains_dir.join(toolchain); let toolchain_meta = fs::symlink_metadata(&toolchain_path)?; let toolchain_path = if verbose { diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 6b760dc6a9..226f5c8032 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -293,6 +293,10 @@ enum ToolchainSubcmd { /// Enable verbose output with toolchain information #[arg(short, long)] verbose: bool, + + /// Force the output to be a single column + #[arg(short, long, conflicts_with = "verbose")] + quiet: bool, }, /// Install or update a given toolchain @@ -623,8 +627,8 @@ pub async fn main() -> Result { } RustupSubcmd::Toolchain { subcmd } => match subcmd { ToolchainSubcmd::Install { opts } => update(cfg, opts).await, - ToolchainSubcmd::List { verbose } => { - handle_epipe(common::list_toolchains(cfg, verbose)) + ToolchainSubcmd::List { verbose, quiet } => { + handle_epipe(common::list_toolchains(cfg, verbose, quiet)) } ToolchainSubcmd::Link { toolchain, path } => { toolchain_link(cfg, &toolchain, &path).await diff --git a/tests/suite/cli-ui/rustup/rustup_toolchain_cmd_list_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_toolchain_cmd_list_cmd_help_flag_stdout.toml index bca9d63246..8669885d01 100644 --- a/tests/suite/cli-ui/rustup/rustup_toolchain_cmd_list_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_toolchain_cmd_list_cmd_help_flag_stdout.toml @@ -8,6 +8,7 @@ Usage: rustup[EXE] toolchain list [OPTIONS] Options: -v, --verbose Enable verbose output with toolchain information + -q, --quiet Force the output to be a single column -h, --help Print help """ stderr = "" diff --git a/tests/suite/cli_rustup.rs b/tests/suite/cli_rustup.rs index 89d4f85a9f..dffad2d092 100644 --- a/tests/suite/cli_rustup.rs +++ b/tests/suite/cli_rustup.rs @@ -841,6 +841,20 @@ fn list_default_toolchain() { }); } +#[test] +fn list_default_toolchain_quiet() { + test(&|config| { + config.with_scenario(Scenario::SimpleV2, &|config| { + config.expect_ok(&["rustup", "default", "nightly"]); + config.expect_ok_ex( + &["rustup", "toolchain", "list", "--quiet"], + for_host!("nightly-{0}\n"), + r"", + ); + }) + }); +} + #[test] fn list_no_default_toolchain() { test(&|config| { From 1384f5bdb30e4b60991e4772b9973477f6c28bd8 Mon Sep 17 00:00:00 2001 From: rami3l Date: Wed, 5 Jun 2024 19:15:52 +0800 Subject: [PATCH 2/2] feat(cli): add `--quiet` to `rustup (target|component) list` --- src/cli/common.rs | 3 ++- src/cli/rustup_mode.rs | 19 ++++++++++++++--- ...mponent_cmd_list_cmd_help_flag_stdout.toml | 1 + ..._target_cmd_list_cmd_help_flag_stdout.toml | 1 + tests/suite/cli_exact.rs | 21 +++++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/cli/common.rs b/src/cli/common.rs index 22ab30e446..d44ee2ebc8 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -377,13 +377,14 @@ pub(super) fn list_items( distributable: DistributableToolchain<'_>, f: impl Fn(&ComponentStatus) -> Option<&str>, installed_only: bool, + quiet: bool, ) -> Result { let mut t = process().stdout().terminal(); for component in distributable.components()? { let Some(name) = f(&component) else { continue }; match (component.available, component.installed, installed_only) { (false, _, _) | (_, false, true) => continue, - (true, true, false) => { + (true, true, false) if !quiet => { t.attr(terminalsource::Attr::Bold)?; writeln!(t.lock(), "{name} (installed)")?; t.reset()?; diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 226f5c8032..84d56671c8 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -385,6 +385,10 @@ enum TargetSubcmd { /// List only installed targets #[arg(long)] installed: bool, + + /// Force the output to be a single column + #[arg(long, short)] + quiet: bool, }, /// Add a target to a Rust toolchain @@ -421,6 +425,10 @@ enum ComponentSubcmd { /// List only installed components #[arg(long)] installed: bool, + + /// Force the output to be a single column + #[arg(long, short)] + quiet: bool, }, /// Add a component to a Rust toolchain @@ -641,7 +649,8 @@ pub async fn main() -> Result { TargetSubcmd::List { toolchain, installed, - } => handle_epipe(target_list(cfg, toolchain, installed).await), + quiet, + } => handle_epipe(target_list(cfg, toolchain, installed, quiet).await), TargetSubcmd::Add { target, toolchain } => target_add(cfg, target, toolchain).await, TargetSubcmd::Remove { target, toolchain } => { target_remove(cfg, target, toolchain).await @@ -651,7 +660,8 @@ pub async fn main() -> Result { ComponentSubcmd::List { toolchain, installed, - } => handle_epipe(component_list(cfg, toolchain, installed).await), + quiet, + } => handle_epipe(component_list(cfg, toolchain, installed, quiet).await), ComponentSubcmd::Add { component, toolchain, @@ -1097,6 +1107,7 @@ async fn target_list( cfg: &Cfg, toolchain: Option, installed_only: bool, + quiet: bool, ) -> Result { // downcasting required because the toolchain files can name any toolchain let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; @@ -1111,6 +1122,7 @@ async fn target_list( }) }, installed_only, + quiet, ) } @@ -1201,10 +1213,11 @@ async fn component_list( cfg: &Cfg, toolchain: Option, installed_only: bool, + quiet: bool, ) -> Result { // downcasting required because the toolchain files can name any toolchain let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?; - common::list_items(distributable, |c| Some(&c.name), installed_only)?; + common::list_items(distributable, |c| Some(&c.name), installed_only, quiet)?; Ok(utils::ExitCode(0)) } diff --git a/tests/suite/cli-ui/rustup/rustup_component_cmd_list_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_component_cmd_list_cmd_help_flag_stdout.toml index 26e8628e5e..e0f3cb89b1 100644 --- a/tests/suite/cli-ui/rustup/rustup_component_cmd_list_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_component_cmd_list_cmd_help_flag_stdout.toml @@ -10,6 +10,7 @@ Options: --toolchain Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more information see `rustup help toolchain` --installed List only installed components + -q, --quiet Force the output to be a single column -h, --help Print help """ stderr = "" diff --git a/tests/suite/cli-ui/rustup/rustup_target_cmd_list_cmd_help_flag_stdout.toml b/tests/suite/cli-ui/rustup/rustup_target_cmd_list_cmd_help_flag_stdout.toml index 091a0acf9f..52f8a98508 100644 --- a/tests/suite/cli-ui/rustup/rustup_target_cmd_list_cmd_help_flag_stdout.toml +++ b/tests/suite/cli-ui/rustup/rustup_target_cmd_list_cmd_help_flag_stdout.toml @@ -10,6 +10,7 @@ Options: --toolchain Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more information see `rustup help toolchain` --installed List only installed targets + -q, --quiet Force the output to be a single column -h, --help Print help """ stderr = "" diff --git a/tests/suite/cli_exact.rs b/tests/suite/cli_exact.rs index 383d8f5bb1..31e8008171 100644 --- a/tests/suite/cli_exact.rs +++ b/tests/suite/cli_exact.rs @@ -615,6 +615,27 @@ fn list_targets() { }); } +#[test] +fn list_targets_quiet() { + test(&|config| { + config.with_scenario(Scenario::SimpleV2, &|config| { + let trip = this_host_triple(); + let mut sorted = [ + trip, + clitools::CROSS_ARCH1.to_string(), + clitools::CROSS_ARCH2.to_string(), + ]; + sorted.sort(); + + let expected = format!("{}\n{}\n{}\n", sorted[0], sorted[1], sorted[2]); + + config.expect_ok(&["rustup", "default", "nightly"]); + config.expect_ok(&["rustup", "target", "add", clitools::CROSS_ARCH1]); + config.expect_ok_ex(&["rustup", "target", "list", "--quiet"], &expected, r""); + }) + }); +} + #[test] fn list_installed_targets() { test(&|config| {