use std::ffi::OsString;
use std::io::ErrorKind;
use std::path::Path;
use std::process::{Command, ExitStatus};
use anyhow::{Context, Result, bail};
pub fn command() -> Command {
Command::new(std::env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")))
}
pub fn run(root: &Path, args: &[&str], envs: &[(&str, &str)]) -> Result<ExitStatus> {
let mut command = command();
command.args(args).current_dir(root);
for (key, value) in envs {
command.env(key, value);
}
match command.status() {
Ok(status) => Ok(status),
Err(err) if err.kind() == ErrorKind::NotFound => {
bail!("`cargo` was not found on your PATH; install Rust from https://rustup.rs")
}
Err(err) => {
Err(err).with_context(|| format!("failed to launch `cargo {}`", args.join(" ")))
}
}
}