Skip to content

Commit cc1de61

Browse files
authored
Merge pull request #1449 from segevfiner/show-active-toolchain-command
Add subcommands to "rustup show" for active-toolchain and it's version
2 parents eae7116 + 0329188 commit cc1de61

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/rustup-cli/help.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ pub static SHOW_HELP: &'static str = r"DISCUSSION:
1717
If there are multiple toolchains installed then all installed
1818
toolchains are listed as well.";
1919

20+
pub static SHOW_ACTIVE_TOOLCHAIN_HELP: &'static str = r"DISCUSSION:
21+
Shows the name of the active toolchain.
22+
23+
This is useful for figuring out the active tool chain from
24+
scripts.
25+
26+
You should use `rustc --print sysroot` to get the sysroot, or
27+
`rustc --version` to get the toolchain version.";
28+
2029
pub static UPDATE_HELP: &'static str = r"DISCUSSION:
2130
With no toolchain specified, the `update` command updates each of
2231
the installed toolchains from the official release channels, then

src/rustup-cli/rustup_mode.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ 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+
(_, _) => show(cfg)?
35+
},
3336
("install", Some(m)) => update(cfg, m)?,
3437
("update", Some(m)) => update(cfg, m)?,
3538
("uninstall", Some(m)) => toolchain_remove(cfg, m)?,
@@ -110,7 +113,13 @@ pub fn cli() -> App<'static, 'static> {
110113
.subcommand(
111114
SubCommand::with_name("show")
112115
.about("Show the active and installed toolchains")
113-
.after_help(SHOW_HELP),
116+
.after_help(SHOW_HELP)
117+
.setting(AppSettings::VersionlessSubcommands)
118+
.setting(AppSettings::DeriveDisplayOrder)
119+
.subcommand(SubCommand::with_name("active-toolchain")
120+
.about("Show the active toolchain")
121+
.after_help(SHOW_ACTIVE_TOOLCHAIN_HELP)
122+
),
114123
)
115124
.subcommand(
116125
SubCommand::with_name("install")
@@ -733,6 +742,19 @@ fn show(cfg: &Cfg) -> Result<()> {
733742
Ok(())
734743
}
735744

745+
fn show_active_toolchain(cfg: &Cfg) -> Result<()> {
746+
let ref cwd = utils::current_dir()?;
747+
match cfg.find_override_toolchain_or_default(cwd)? {
748+
Some((ref toolchain, _)) => {
749+
println!("{}", toolchain.name());
750+
},
751+
None => {
752+
// Print nothing
753+
}
754+
}
755+
Ok(())
756+
}
757+
736758
fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
737759
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
738760

tests/cli-rustup.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,34 @@ 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+
848876
// #846
849877
#[test]
850878
fn set_default_host() {

0 commit comments

Comments
 (0)