Skip to content
Snippets Groups Projects
Commit c6e49a0f authored by Simon Sapin's avatar Simon Sapin
Browse files

Extract subprocess handling into a new function

parent 0a2c98d9
No related branches found
No related tags found
1 merge request!1Initial implementation
......@@ -53,12 +53,12 @@
.to_str()
.ok_or("Non-Unicode base name")?;
let today = time::OffsetDateTime::now_utc().date();
let hg_id_output = process::Command::new("hg")
.arg("id")
.arg("--id")
.arg("-R")
.arg(&self.directory)
.output()?;
let id = if hg_id_output.status.success() {
format!("_{}", str::from_utf8(&hg_id_output.stdout)?.trim())
let id = if let Some(stdout) = subprocess_output(
process::Command::new("hg")
.arg("id")
.arg("--id")
.arg("-R")
.arg(&self.directory),
)? {
format!("_{}", stdout)
} else {
......@@ -64,6 +64,4 @@
} else {
let hg_stderr = str::from_utf8(&hg_id_output.stderr)?;
eprintln!("{}", style(hg_stderr.trim()).red());
println!("Not a Mercurial repository?");
format!("")
};
......@@ -167,6 +165,19 @@
}
}
/// If the proccess has a successful exit code, return its trimmed stdout
/// If the process has an error exit code, print its stderr in red and return None
/// Return an error if the spawning the process or UTF-8 decoding fails
fn subprocess_output(command: &mut process::Command) -> Result<Option<String>> {
let out = command.output()?;
if out.status.success() {
Ok(Some(str::from_utf8(&out.stdout)?.trim().to_owned()))
} else {
eprintln!("{}", style(str::from_utf8(&out.stderr)?.trim()).red());
Ok(None)
}
}
struct Tee<A, B>(A, B);
impl<A: io::Write, B: io::Write> io::Write for Tee<A, B> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment