Add check_output

This commit is contained in:
Roey Darwish Dror
2019-01-01 22:22:07 +02:00
parent 39d4100591
commit 1b2308aaef
5 changed files with 43 additions and 54 deletions

View File

@@ -176,3 +176,20 @@ impl Check for ExecutorExitStatus {
}
}
}
/// Extension methods for `std::process::Command`
pub trait CommandExt {
/// Run the command, wait for it to complete, check the return code and decode the output as UTF-8.
fn check_output(&mut self) -> Result<String, Error>;
}
impl CommandExt for Command {
fn check_output(&mut self) -> Result<String, Error> {
let output = self.output().context(ErrorKind::ProcessExecution)?;
let status = output.status;
if !status.success() {
Err(ErrorKind::ProcessFailed(status))?
}
Ok(String::from_utf8(output.stdout).context(ErrorKind::ProcessExecution)?)
}
}