Skip to content

Commit

Permalink
Auto merge of #949 - RalfJung:ask-to-run, r=RalfJung
Browse files Browse the repository at this point in the history
factor ask-to-run-command into helper function
  • Loading branch information
bors committed Sep 14, 2019
2 parents 7847f79 + b245cb6 commit 9a14624
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions src/bin/cargo-miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,28 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
Some((major, minor, patch))
}

fn ask(question: &str) {
let mut buf = String::new();
print!("{} [Y/n] ", question);
io::stdout().flush().unwrap();
io::stdin().read_line(&mut buf).unwrap();
match buf.trim().to_lowercase().as_ref() {
// Proceed.
"" | "y" | "yes" => {},
"n" | "no" => show_error(format!("Aborting as per your request")),
a => show_error(format!("I do not understand `{}`", a))
};
fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
if ask {
let mut buf = String::new();
print!("I will run `{:?}` to {}. Proceed? [Y/n] ", cmd, text);
io::stdout().flush().unwrap();
io::stdin().read_line(&mut buf).unwrap();
match buf.trim().to_lowercase().as_ref() {
// Proceed.
"" | "y" | "yes" => {},
"n" | "no" => show_error(format!("Aborting as per your request")),
a => show_error(format!("I do not understand `{}`", a))
};
} else {
println!("Running `{:?}` to {}.", cmd, text);
}

if cmd.status()
.expect(&format!("failed to execute {:?}", cmd))
.success().not()
{
show_error(format!("Failed to {}", text));
}
}

/// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
Expand All @@ -244,18 +255,9 @@ fn setup(ask_user: bool) {

// First, we need xargo.
if xargo_version().map_or(true, |v| v < (0, 3, 16)) {
if ask_user {
ask("It seems you do not have a recent enough xargo installed. I will run `cargo install xargo -f`. Proceed?");
} else {
println!("Installing xargo: `cargo install xargo -f`");
}

if cargo().args(&["install", "xargo", "-f"]).status()
.expect("failed to install xargo")
.success().not()
{
show_error(format!("Failed to install xargo"));
}
let mut cmd = cargo();
cmd.args(&["install", "xargo", "-f"]);
ask_to_run(cmd, ask_user, "install a recent enough xargo");
}

// Then, unless `XARGO_RUST_SRC` is set, we also need rust-src.
Expand All @@ -267,17 +269,9 @@ fn setup(ask_user: bool) {
let sysroot = std::str::from_utf8(&sysroot).unwrap();
let src = Path::new(sysroot.trim_end_matches('\n')).join("lib").join("rustlib").join("src");
if !src.exists() {
if ask_user {
ask("It seems you do not have the rust-src component installed. I will run `rustup component add rust-src` for the selected toolchain. Proceed?");
} else {
println!("Installing rust-src component: `rustup component add rust-src`");
}
if !Command::new("rustup").args(&["component", "add", "rust-src"]).status()
.expect("failed to install rust-src component")
.success()
{
show_error(format!("Failed to install rust-src component"));
}
let mut cmd = Command::new("rustup");
cmd.args(&["component", "add", "rust-src"]);
ask_to_run(cmd, ask_user, "install the rustc-src component for the selected toolchain");
}
}

Expand Down

0 comments on commit 9a14624

Please sign in to comment.