use crate::error::Result;
use std::path::PathBuf;
pub fn run_check(workspace_path: &str, check: &str) -> Result<()> {
let args = match check {
"test" => &["cargo", "test", "--all-features"][..],
"clippy" => &[
"cargo",
"clippy",
"--all-targets",
"--all-features",
"--",
"-D",
"warnings",
][..],
"fmt" => &["cargo", "fmt", "--check"][..],
"doc" => &["cargo", "doc", "--no-deps", "--all-features"][..],
"build" => &["cargo", "build", "--all-features"][..],
_ => {
return Err(crate::error::Error::Config(format!(
"Unknown check: {check}"
)));
}
};
run_command(workspace_path, args)?;
Ok(())
}
fn run_command(workspace_path: &str, args: &[&str]) -> Result<std::process::ExitStatus> {
use command_group::CommandGroup;
let output = std::process::Command::new(args[0])
.args(&args[1..])
.current_dir(PathBuf::from(workspace_path))
.group_spawn()
.map_err(|e| crate::error::Error::Io(format!("Failed to spawn {args:?}: {e}")))?
.wait()
.map_err(|e| crate::error::Error::Io(format!("Failed to wait for {args:?}: {e}")))?;
if output.success() {
Ok(output)
} else {
Err(crate::error::Error::CheckFailed(format!(
"Command {:?} failed with exit code {:?}",
args,
output.code()
)))
}
}