Skip to content

Commit 9a14624

Browse files
committed
Auto merge of #949 - RalfJung:ask-to-run, r=RalfJung
factor ask-to-run-command into helper function
2 parents 7847f79 + b245cb6 commit 9a14624

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

src/bin/cargo-miri.rs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,28 @@ fn xargo_version() -> Option<(u32, u32, u32)> {
218218
Some((major, minor, patch))
219219
}
220220

221-
fn ask(question: &str) {
222-
let mut buf = String::new();
223-
print!("{} [Y/n] ", question);
224-
io::stdout().flush().unwrap();
225-
io::stdin().read_line(&mut buf).unwrap();
226-
match buf.trim().to_lowercase().as_ref() {
227-
// Proceed.
228-
"" | "y" | "yes" => {},
229-
"n" | "no" => show_error(format!("Aborting as per your request")),
230-
a => show_error(format!("I do not understand `{}`", a))
231-
};
221+
fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
222+
if ask {
223+
let mut buf = String::new();
224+
print!("I will run `{:?}` to {}. Proceed? [Y/n] ", cmd, text);
225+
io::stdout().flush().unwrap();
226+
io::stdin().read_line(&mut buf).unwrap();
227+
match buf.trim().to_lowercase().as_ref() {
228+
// Proceed.
229+
"" | "y" | "yes" => {},
230+
"n" | "no" => show_error(format!("Aborting as per your request")),
231+
a => show_error(format!("I do not understand `{}`", a))
232+
};
233+
} else {
234+
println!("Running `{:?}` to {}.", cmd, text);
235+
}
236+
237+
if cmd.status()
238+
.expect(&format!("failed to execute {:?}", cmd))
239+
.success().not()
240+
{
241+
show_error(format!("Failed to {}", text));
242+
}
232243
}
233244

234245
/// 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) {
244255

245256
// First, we need xargo.
246257
if xargo_version().map_or(true, |v| v < (0, 3, 16)) {
247-
if ask_user {
248-
ask("It seems you do not have a recent enough xargo installed. I will run `cargo install xargo -f`. Proceed?");
249-
} else {
250-
println!("Installing xargo: `cargo install xargo -f`");
251-
}
252-
253-
if cargo().args(&["install", "xargo", "-f"]).status()
254-
.expect("failed to install xargo")
255-
.success().not()
256-
{
257-
show_error(format!("Failed to install xargo"));
258-
}
258+
let mut cmd = cargo();
259+
cmd.args(&["install", "xargo", "-f"]);
260+
ask_to_run(cmd, ask_user, "install a recent enough xargo");
259261
}
260262

261263
// Then, unless `XARGO_RUST_SRC` is set, we also need rust-src.
@@ -267,17 +269,9 @@ fn setup(ask_user: bool) {
267269
let sysroot = std::str::from_utf8(&sysroot).unwrap();
268270
let src = Path::new(sysroot.trim_end_matches('\n')).join("lib").join("rustlib").join("src");
269271
if !src.exists() {
270-
if ask_user {
271-
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?");
272-
} else {
273-
println!("Installing rust-src component: `rustup component add rust-src`");
274-
}
275-
if !Command::new("rustup").args(&["component", "add", "rust-src"]).status()
276-
.expect("failed to install rust-src component")
277-
.success()
278-
{
279-
show_error(format!("Failed to install rust-src component"));
280-
}
272+
let mut cmd = Command::new("rustup");
273+
cmd.args(&["component", "add", "rust-src"]);
274+
ask_to_run(cmd, ask_user, "install the rustc-src component for the selected toolchain");
281275
}
282276
}
283277

0 commit comments

Comments
 (0)