2020-05-27 10:04:42 +02:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
use crate::error::ParseError;
|
|
|
|
|
|
|
|
|
|
/// Execute an arbitrary Unix command and return the stdout as a `String` if
|
|
|
|
|
/// successful.
|
2021-03-13 18:51:31 +01:00
|
|
|
pub fn execute(command: &str, args: &[&str]) -> Result<String, ParseError> {
|
2020-05-27 10:04:42 +02:00
|
|
|
let output = Command::new(command).args(args).output()?;
|
|
|
|
|
|
|
|
|
|
if !output.status.success() {
|
|
|
|
|
let msg = String::from_utf8_lossy(&output.stderr);
|
|
|
|
|
return Err(ParseError::ProcessFailure(format!(
|
|
|
|
|
"Process failure: {} {}, error {}",
|
|
|
|
|
command,
|
|
|
|
|
args.join(" "),
|
|
|
|
|
msg
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
|
|
|
|
}
|