Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add --quiet to rustup toolchain list and friends #3862

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,14 @@ pub(super) fn list_items(
distributable: DistributableToolchain<'_>,
f: impl Fn(&ComponentStatus) -> Option<&str>,
installed_only: bool,
quiet: bool,
) -> Result<utils::ExitCode> {
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()?;
Expand All @@ -397,7 +398,7 @@ pub(super) fn list_items(
Ok(utils::ExitCode(0))
}

pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCode> {
pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool, quiet: bool) -> Result<utils::ExitCode> {
let toolchains = cfg.list_toolchains()?;
if toolchains.is_empty() {
writeln!(process().stdout().lock(), "no installed toolchains")?;
Expand All @@ -422,6 +423,7 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCod
is_default_toolchain,
is_active_toolchain,
verbose,
quiet,
)
.context("Failed to list toolchains' directories")?;
}
Expand All @@ -433,7 +435,13 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCod
is_default: bool,
is_active: bool,
verbose: bool,
quiet: bool,
) -> 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 {
Expand Down
27 changes: 22 additions & 5 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -381,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
Expand Down Expand Up @@ -417,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
Expand Down Expand Up @@ -623,8 +635,8 @@ pub async fn main() -> Result<utils::ExitCode> {
}
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
Expand All @@ -637,7 +649,8 @@ pub async fn main() -> Result<utils::ExitCode> {
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
Expand All @@ -647,7 +660,8 @@ pub async fn main() -> Result<utils::ExitCode> {
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,
Expand Down Expand Up @@ -1093,6 +1107,7 @@ async fn target_list(
cfg: &Cfg,
toolchain: Option<PartialToolchainDesc>,
installed_only: bool,
quiet: bool,
) -> Result<utils::ExitCode> {
// downcasting required because the toolchain files can name any toolchain
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
Expand All @@ -1107,6 +1122,7 @@ async fn target_list(
})
},
installed_only,
quiet,
)
}

Expand Down Expand Up @@ -1197,10 +1213,11 @@ async fn component_list(
cfg: &Cfg,
toolchain: Option<PartialToolchainDesc>,
installed_only: bool,
quiet: bool,
) -> Result<utils::ExitCode> {
// 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))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Options:
--toolchain <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 = ""
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Options:
--toolchain <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 = ""
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
21 changes: 21 additions & 0 deletions tests/suite/cli_exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
14 changes: 14 additions & 0 deletions tests/suite/cli_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
Loading