]> git.lizzy.rs Git - rust.git/commitdiff
factor ask-to-run-command into helper function
authorRalf Jung <post@ralfj.de>
Fri, 13 Sep 2019 08:39:36 +0000 (10:39 +0200)
committerRalf Jung <post@ralfj.de>
Fri, 13 Sep 2019 08:39:36 +0000 (10:39 +0200)
src/bin/cargo-miri.rs

index 8749e5b7eed93477d4b85d4c7d231be7279b5552..43e8761d48c17c1be1f63d545c7165319263c876 100644 (file)
@@ -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
@@ -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.
@@ -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");
         }
     }