Skip to content

Commit 250328f

Browse files
committed
Add subcommands to "rustup show" for active-toolchain and it's version
This should be useful to shell scripts (via command substitution) and other programs that need this information. Related: #450
1 parent bb6f65f commit 250328f

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

src/rustup-cli/rustup_mode.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub fn main() -> Result<()> {
2929
cfg.check_metadata_version()?;
3030

3131
match matches.subcommand() {
32-
("show", Some(_)) => show(cfg)?,
32+
("show", Some(c)) => match c.subcommand() {
33+
("active-toolchain", Some(_)) => show_active_toolchain(cfg)?,
34+
("active-toolchain-version", Some(_)) => show_active_toolchain_version(cfg)?,
35+
(_, _) => show(cfg)?
36+
},
3337
("install", Some(m)) => update(cfg, m)?,
3438
("update", Some(m)) => update(cfg, m)?,
3539
("uninstall", Some(m)) => toolchain_remove(cfg, m)?,
@@ -110,7 +114,15 @@ pub fn cli() -> App<'static, 'static> {
110114
.subcommand(
111115
SubCommand::with_name("show")
112116
.about("Show the active and installed toolchains")
113-
.after_help(SHOW_HELP),
117+
.after_help(SHOW_HELP)
118+
.setting(AppSettings::VersionlessSubcommands)
119+
.setting(AppSettings::DeriveDisplayOrder)
120+
.subcommand(SubCommand::with_name("active-toolchain")
121+
.about("Show the active toolchain")
122+
)
123+
.subcommand(SubCommand::with_name("active-toolchain-version")
124+
.about("Show the active toolchain version")
125+
),
114126
)
115127
.subcommand(
116128
SubCommand::with_name("install")
@@ -733,6 +745,32 @@ fn show(cfg: &Cfg) -> Result<()> {
733745
Ok(())
734746
}
735747

748+
fn show_active_toolchain(cfg: &Cfg) -> Result<()> {
749+
let ref cwd = utils::current_dir()?;
750+
match cfg.find_override_toolchain_or_default(cwd)? {
751+
Some((ref toolchain, _)) => {
752+
println!("{}", toolchain.name());
753+
},
754+
None => {
755+
// Print nothing
756+
}
757+
}
758+
Ok(())
759+
}
760+
761+
fn show_active_toolchain_version(cfg: &Cfg) -> Result<()> {
762+
let ref cwd = utils::current_dir()?;
763+
match cfg.find_override_toolchain_or_default(cwd)? {
764+
Some((ref toolchain, _)) => {
765+
println!("{}", common::rustc_version(toolchain));
766+
},
767+
None => {
768+
// Print nothing
769+
}
770+
}
771+
Ok(())
772+
}
773+
736774
fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
737775
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
738776

tests/cli-rustup.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,60 @@ fn show_toolchain_env_not_installed() {
845845
});
846846
}
847847

848+
#[test]
849+
fn show_active_toolchain() {
850+
setup(&|config| {
851+
expect_ok(config, &["rustup", "default", "nightly"]);
852+
expect_ok_ex(
853+
config,
854+
&["rustup", "show", "active-toolchain"],
855+
for_host!(
856+
r"nightly-{0}
857+
"
858+
),
859+
r"",
860+
);
861+
});
862+
}
863+
864+
#[test]
865+
fn show_active_toolchain_none() {
866+
setup(&|config| {
867+
expect_ok_ex(
868+
config,
869+
&["rustup", "show", "active-toolchain"],
870+
r"",
871+
r"",
872+
);
873+
});
874+
}
875+
876+
#[test]
877+
fn show_active_toolchain_version() {
878+
setup(&|config| {
879+
expect_ok(config, &["rustup", "default", "nightly"]);
880+
expect_ok_ex(
881+
config,
882+
&["rustup", "show", "active-toolchain-version"],
883+
r"1.3.0 (hash-n-2)
884+
",
885+
r"",
886+
);
887+
});
888+
}
889+
890+
#[test]
891+
fn show_active_toolchain_version_none() {
892+
setup(&|config| {
893+
expect_ok_ex(
894+
config,
895+
&["rustup", "show", "active-toolchain-version"],
896+
r"",
897+
r"",
898+
);
899+
});
900+
}
901+
848902
// #846
849903
#[test]
850904
fn set_default_host() {

0 commit comments

Comments
 (0)