#[macro_use]
extern crate failure;
use failure::Error;
use std::process::Command;
use std::process::Output;
#[derive(Fail, Debug)]
#[fail(display = "Command failed: {}, output: {:?}", command, output)]
pub struct CommandFail {
command: String,
output: Output,
}
impl CommandFail {
pub fn command(&self) -> &str {
&self.command
}
pub fn output(&self) -> &Output {
&self.output
}
}
type Result<T> = std::result::Result<T, Error>;
pub fn run_shell_command(cmd: &str) -> Result<String> {
let output = if cfg!(target_os = "windows") {
Command::new("cmd").arg("/C").arg(cmd).output()?
} else {
Command::new("sh").arg("-c").arg(cmd).output()?
};
if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(CommandFail {
command: cmd.into(),
output,
}
.into())
}
}