Skip to content

Commit

Permalink
Support --toolchain in front of rustup which
Browse files Browse the repository at this point in the history
  • Loading branch information
BeniCheni committed Sep 30, 2019
1 parent a2f8fce commit 63b814b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn main() -> Result<()> {
let matches = cli().get_matches();
let verbose = matches.is_present("verbose");
let quiet = matches.is_present("quiet");
let toolchain = matches.value_of("toolchain");
let cfg = &common::set_globals(verbose, quiet)?;

if maybe_upgrade_data(cfg, &matches)? {
Expand Down Expand Up @@ -79,7 +80,7 @@ pub fn main() -> Result<()> {
(_, _) => unreachable!(),
},
("run", Some(m)) => run(cfg, m)?,
("which", Some(m)) => which(cfg, m)?,
("which", Some(m)) => which(cfg, m, toolchain)?,
("doc", Some(m)) => doc(cfg, m)?,
("man", Some(m)) => man(cfg, m)?,
("self", Some(c)) => match c.subcommand() {
Expand Down Expand Up @@ -817,16 +818,20 @@ fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
process::exit(c)
}

fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
fn which(cfg: &Cfg, m: &ArgMatches<'_>, toolchain_by_root_cmd: Option<&str>) -> Result<()> {
let binary = m.value_of("command").expect("");
let toolchain_provided = m.is_present("toolchain");
let binary_path = if toolchain_provided {
let toolchain = m.value_of("toolchain").expect("");
cfg.which_binary_by_toolchain(toolchain, binary)?
let binary_path = if let Some(toolchain_by_root_cmd) = toolchain_by_root_cmd {
cfg.which_binary_by_toolchain(toolchain_by_root_cmd, binary)?
.expect("binary not found")
} else {
cfg.which_binary(&utils::current_dir()?, binary)?
.expect("binary not found")
if m.is_present("toolchain") {
let toolchain = m.value_of("toolchain").expect("");
cfg.which_binary_by_toolchain(toolchain, binary)?
.expect("binary not found")
} else {
cfg.which_binary(&utils::current_dir()?, binary)?
.expect("binary not found")
}
};

utils::assert_is_file(&binary_path)?;
Expand Down
12 changes: 12 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,5 +903,17 @@ fn which() {
&["rustup", "which", "--toolchain=custom-2", "rustc"],
"/toolchains/custom-2/bin/rustc",
);
#[cfg(windows)]
expect_stdout_ok(
config,
&["rustup", "--toolchain=custom-2", "which", "rustc"],
"\\toolchains\\custom-2\\bin\\rustc",
);
#[cfg(not(windows))]
expect_stdout_ok(
config,
&["rustup", "--toolchain=custom-2", "which", "rustc"],
"/toolchains/custom-2/bin/rustc",
);
});
}

0 comments on commit 63b814b

Please sign in to comment.