use failure::Error;
use install::Tool;
use log::info;
use std::process::{Command, Stdio};
pub fn new_command(program: &str) -> Command {
if cfg!(windows) {
let mut cmd = Command::new("cmd");
cmd.arg("/c").arg(program);
cmd
} else {
Command::new(program)
}
}
pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
info!("Running {:?}", command);
let status = command.status()?;
if status.success() {
Ok(())
} else {
bail!(
"failed to execute `{}`: exited with {}\n full command: {:?}",
command_name,
status,
command,
)
}
}
pub fn run_capture_stdout(mut command: Command, command_name: &Tool) -> Result<String, Error> {
info!("Running {:?}", command);
let output = command
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.output()?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
bail!(
"failed to execute `{}`: exited with {}\n full command: {:?}",
command_name,
output.status,
command,
)
}
}