Skip to content

Commit 8d36d35

Browse files
authored
Merge pull request #6 from rust-lang/stream-cmd
Stream output of certain commands to the terminal
2 parents 4af200d + cd171c6 commit 8d36d35

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/sync.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::SyncContext;
22
use crate::josh::JoshProxy;
3-
use crate::utils::run_command;
43
use crate::utils::run_command_at;
54
use crate::utils::{ensure_clean_git_state, prompt};
5+
use crate::utils::{run_command, stream_command};
66
use anyhow::{Context, Error};
77
use std::path::{Path, PathBuf};
88

@@ -158,7 +158,8 @@ This merge was created using https://github.com/rust-lang/josh-sync.
158158
);
159159

160160
// Merge the fetched commit.
161-
run_command(&[
161+
// It is useful to print stdout/stderr here, because it shows the git diff summary
162+
stream_command(&[
162163
"git",
163164
"merge",
164165
"FETCH_HEAD",
@@ -301,7 +302,8 @@ fn prepare_rustc_checkout() -> anyhow::Result<PathBuf> {
301302
println!(
302303
"Cloning rustc into `{path}`. Use RUSTC_GIT environment variable to override the ___location of the checkout"
303304
);
304-
run_command(&[
305+
// Stream stdout/stderr to the terminal, so that the user sees clone progress
306+
stream_command(&[
305307
"git",
306308
"clone",
307309
"--filter=blob:none",

src/utils.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@ pub fn run_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<Str
66
run_command_at(args, &std::env::current_dir()?)
77
}
88

9+
/// Run command while streaming stdout and stderr to the terminal.
10+
pub fn stream_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<()> {
11+
run_command_inner(args, &std::env::current_dir()?, false)?;
12+
Ok(())
13+
}
14+
915
pub fn run_command_at<'a, Args: AsRef<[&'a str]>>(
1016
args: Args,
1117
workdir: &Path,
18+
) -> anyhow::Result<String> {
19+
run_command_inner(args, workdir, true)
20+
}
21+
22+
fn run_command_inner<'a, Args: AsRef<[&'a str]>>(
23+
args: Args,
24+
workdir: &Path,
25+
capture: bool,
1226
) -> anyhow::Result<String> {
1327
let args = args.as_ref();
1428

@@ -17,16 +31,32 @@ pub fn run_command_at<'a, Args: AsRef<[&'a str]>>(
1731
cmd.args(&args[1..]);
1832

1933
eprintln!("+ {cmd:?}");
20-
let out = cmd.output().expect("command failed");
21-
let stdout = String::from_utf8_lossy(out.stdout.trim_ascii()).to_string();
22-
let stderr = String::from_utf8_lossy(out.stderr.trim_ascii()).to_string();
23-
if !out.status.success() {
24-
Err(anyhow::anyhow!(
25-
"Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n{stdout}\nSTDERR:\n{stderr}",
26-
out.status.code()
27-
))
34+
if capture {
35+
let out = cmd.output().expect("command failed");
36+
let stdout = String::from_utf8_lossy(out.stdout.trim_ascii()).to_string();
37+
let stderr = String::from_utf8_lossy(out.stderr.trim_ascii()).to_string();
38+
if !out.status.success() {
39+
Err(anyhow::anyhow!(
40+
"Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n{stdout}\nSTDERR:\n{stderr}",
41+
out.status.code()
42+
))
43+
} else {
44+
Ok(stdout)
45+
}
2846
} else {
29-
Ok(stdout)
47+
let status = cmd
48+
.spawn()
49+
.expect("cannot spawn command")
50+
.wait()
51+
.expect("command failed");
52+
if !status.success() {
53+
Err(anyhow::anyhow!(
54+
"Command `{cmd:?}` failed with exit code {:?}",
55+
status.code()
56+
))
57+
} else {
58+
Ok(String::new())
59+
}
3060
}
3161
}
3262

0 commit comments

Comments
 (0)