Skip to content

Commit 26ed8d8

Browse files
author
Trevor Miranda
committed
Filtering of component list by available, required, and installed
1 parent b697df1 commit 26ed8d8

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

src/rustup-cli/common.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,25 @@ pub fn list_targets(toolchain: &Toolchain) -> Result<()> {
320320
Ok(())
321321
}
322322

323-
pub fn list_components(toolchain: &Toolchain) -> Result<()> {
323+
pub enum ComponentFilter {
324+
Required,
325+
Available,
326+
Installed,
327+
None,
328+
}
329+
330+
pub fn list_components(toolchain: &Toolchain, filter: &ComponentFilter) -> Result<()> {
324331
let mut t = term2::stdout();
325-
for component in toolchain.list_components()? {
332+
let components = toolchain
333+
.list_components()?
334+
.into_iter()
335+
.filter(|c| match filter {
336+
ComponentFilter::Required => c.required,
337+
ComponentFilter::Available => c.available && !c.installed,
338+
ComponentFilter::Installed => c.installed,
339+
ComponentFilter::None => c.required || c.available || c.installed,
340+
});
341+
for component in components {
326342
let name = component.name;
327343
if component.required {
328344
let _ = t.attr(term2::Attr::Bold);

src/rustup-cli/help.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,6 @@ r"DISCUSSION:
254254
pub static TOOLCHAIN_ARG_HELP: &'static str = "Toolchain name, such as 'stable', 'nightly', \
255255
or '1.8.0'. For more information see `rustup \
256256
help toolchain`";
257+
258+
pub static FILTER_ARG_HELP: &'static str =
259+
"Component status: 'required', 'available', or 'installed'";

src/rustup-cli/rustup_mode.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ pub fn cli() -> App<'static, 'static> {
296296
.help(TOOLCHAIN_ARG_HELP)
297297
.long("toolchain")
298298
.takes_value(true),
299+
)
300+
.arg(
301+
Arg::with_name("filter")
302+
.help(FILTER_ARG_HELP)
303+
.long("filter")
304+
.takes_value(true),
299305
),
300306
)
301307
.subcommand(
@@ -825,7 +831,13 @@ fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
825831
fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
826832
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
827833

828-
common::list_components(&toolchain)
834+
let filter = match m.value_of("filter") {
835+
Some("required") => common::ComponentFilter::Required,
836+
Some("available") => common::ComponentFilter::Available,
837+
Some("installed") => common::ComponentFilter::Installed,
838+
_ => common::ComponentFilter::None,
839+
};
840+
common::list_components(&toolchain, &filter)
829841
}
830842

831843
fn component_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {

tests/cli-rustup.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,80 @@ fn add_remove_multiple_components() {
10931093
});
10941094
}
10951095

1096+
#[test]
1097+
fn list_components_filter() {
1098+
setup(&|config| {
1099+
expect_err(
1100+
config,
1101+
&["rustup", "component", "list", "--filter", "required"],
1102+
"error: no default toolchain configured",
1103+
);
1104+
expect_ok(config, &["rustup", "default", "nightly"]);
1105+
expect_ok_ex(
1106+
config,
1107+
&["rustup", "component", "list", "--filter", "required"],
1108+
for_host!(
1109+
r"cargo-{0} (default)
1110+
rust-docs-{0} (default)
1111+
rust-std-{0} (default)
1112+
rustc-{0} (default)
1113+
"
1114+
),
1115+
"",
1116+
);
1117+
expect_ok(
1118+
config,
1119+
&["rustup", "component", "list", "--filter", "available"],
1120+
);
1121+
expect_ok_ex(
1122+
config,
1123+
&["rustup", "component", "list", "--filter", "installed"],
1124+
for_host!(
1125+
"cargo-{0} (default)
1126+
rust-docs-{0} (default)
1127+
rust-std-{0} (default)
1128+
rustc-{0} (default)
1129+
"
1130+
),
1131+
"",
1132+
);
1133+
expect_ok(config, &["rustup", "component", "add", "rls"]);
1134+
expect_ok_ex(
1135+
config,
1136+
&["rustup", "component", "list", "--filter", "installed"],
1137+
for_host!(
1138+
"cargo-{0} (default)
1139+
rls-{0} (installed)
1140+
rust-docs-{0} (default)
1141+
rust-std-{0} (default)
1142+
rustc-{0} (default)
1143+
"
1144+
),
1145+
"",
1146+
);
1147+
expect_err(
1148+
config,
1149+
&["rustup", "component", "list", "--filter"],
1150+
"error: The argument '--filter <filter>' requires a value but none was supplied",
1151+
);
1152+
1153+
// Don't show installed/default components in available filter
1154+
let mut cmd = clitools::cmd(
1155+
config,
1156+
"rustup",
1157+
&["component", "list", "--filter", "available"],
1158+
);
1159+
clitools::env(config, &mut cmd);
1160+
let out = cmd.output().unwrap();
1161+
assert!(!String::from_utf8(out.stdout.clone())
1162+
.unwrap()
1163+
.contains("(installed)"));
1164+
assert!(!String::from_utf8(out.stdout.clone())
1165+
.unwrap()
1166+
.contains("(default)"));
1167+
});
1168+
}
1169+
10961170
// Run without setting RUSTUP_HOME, with setting HOME and USERPROFILE
10971171
fn run_no_home(config: &Config, args: &[&str], env: &[(&str, &str)]) -> process::Output {
10981172
let home_dir_str = &format!("{}", config.homedir.display());

0 commit comments

Comments
 (0)