Skip to content

Stream output of certain commands to the terminal #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::SyncContext;
use crate::josh::JoshProxy;
use crate::utils::run_command;
use crate::utils::run_command_at;
use crate::utils::{ensure_clean_git_state, prompt};
use crate::utils::{run_command, stream_command};
use anyhow::{Context, Error};
use std::path::{Path, PathBuf};

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

// Merge the fetched commit.
run_command(&[
// It is useful to print stdout/stderr here, because it shows the git diff summary
stream_command(&[
"git",
"merge",
"FETCH_HEAD",
Expand Down Expand Up @@ -301,7 +302,8 @@ fn prepare_rustc_checkout() -> anyhow::Result<PathBuf> {
println!(
"Cloning rustc into `{path}`. Use RUSTC_GIT environment variable to override the location of the checkout"
);
run_command(&[
// Stream stdout/stderr to the terminal, so that the user sees clone progress
stream_command(&[
"git",
"clone",
"--filter=blob:none",
Expand Down
48 changes: 39 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@ pub fn run_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<Str
run_command_at(args, &std::env::current_dir()?)
}

/// Run command while streaming stdout and stderr to the terminal.
pub fn stream_command<'a, Args: AsRef<[&'a str]>>(args: Args) -> anyhow::Result<()> {
run_command_inner(args, &std::env::current_dir()?, false)?;
Ok(())
}

pub fn run_command_at<'a, Args: AsRef<[&'a str]>>(
args: Args,
workdir: &Path,
) -> anyhow::Result<String> {
run_command_inner(args, workdir, true)
}

fn run_command_inner<'a, Args: AsRef<[&'a str]>>(
args: Args,
workdir: &Path,
capture: bool,
) -> anyhow::Result<String> {
let args = args.as_ref();

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

eprintln!("+ {cmd:?}");
let out = cmd.output().expect("command failed");
let stdout = String::from_utf8_lossy(out.stdout.trim_ascii()).to_string();
let stderr = String::from_utf8_lossy(out.stderr.trim_ascii()).to_string();
if !out.status.success() {
Err(anyhow::anyhow!(
"Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n{stdout}\nSTDERR:\n{stderr}",
out.status.code()
))
if capture {
let out = cmd.output().expect("command failed");
let stdout = String::from_utf8_lossy(out.stdout.trim_ascii()).to_string();
let stderr = String::from_utf8_lossy(out.stderr.trim_ascii()).to_string();
if !out.status.success() {
Err(anyhow::anyhow!(
"Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n{stdout}\nSTDERR:\n{stderr}",
out.status.code()
))
} else {
Ok(stdout)
}
} else {
Ok(stdout)
let status = cmd
.spawn()
.expect("cannot spawn command")
.wait()
.expect("command failed");
if !status.success() {
Err(anyhow::anyhow!(
"Command `{cmd:?}` failed with exit code {:?}",
status.code()
))
} else {
Ok(String::new())
}
}
}

Expand Down
Loading