Skip to content

Commit ad75b2f

Browse files
xtask: Add util::run_cmd_capture_stdout
1 parent bc04551 commit ad75b2f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

xtask/src/util.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{bail, Result};
1+
use anyhow::{bail, Context, Result};
22
use std::process::Command;
33

44
/// Format a `Command` as a `String.
@@ -44,6 +44,19 @@ pub fn run_cmd(mut cmd: Command) -> Result<()> {
4444
}
4545
}
4646

47+
/// Print a `Command` and run it, then check that it completes
48+
/// successfully. Return the command's stdout as a `String`.
49+
pub fn run_cmd_get_stdout(mut cmd: Command) -> Result<String> {
50+
println!("run_cmd: '{}'", command_to_string(&cmd));
51+
52+
let output = cmd.output()?;
53+
if output.status.success() {
54+
String::from_utf8(output.stdout).context("command output is not utf-8")
55+
} else {
56+
bail!("command failed: {}", output.status);
57+
}
58+
}
59+
4760
#[cfg(test)]
4861
mod tests {
4962
use super::*;

0 commit comments

Comments
 (0)