mirror of
https://seed.radicle.dev/rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE.git
synced 2026-11-21 18:40:47 +08:00
57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
use std::{
|
|
path::{Path, PathBuf},
|
|
process::Command,
|
|
};
|
|
|
|
use radicle_ci_broker::msg::{MessageError, Response};
|
|
|
|
use crate::runlog::{RunLog, RunLogError};
|
|
|
|
/// Run a command in a directory.
|
|
pub fn runcmd(run_log: &mut RunLog, argv: &[&str], cwd: &Path) -> Result<(), RunCmdError> {
|
|
assert!(!argv.is_empty());
|
|
let argv0 = argv[0];
|
|
let output = Command::new(argv0)
|
|
.args(&argv[1..])
|
|
.current_dir(cwd)
|
|
.output()
|
|
.map_err(|e| RunCmdError::Command(argv.iter().map(|s| s.to_string()).collect(), e))?;
|
|
|
|
let exit = output.status;
|
|
|
|
run_log.runcmd(
|
|
argv,
|
|
&cwd.canonicalize()
|
|
.map_err(|e| RunCmdError::Canonicalize(cwd.into(), e))?,
|
|
exit.code().unwrap(),
|
|
&output.stdout,
|
|
&output.stderr,
|
|
);
|
|
|
|
if !exit.success() {
|
|
return Err(RunCmdError::CommandFailed(
|
|
exit.code().unwrap(),
|
|
argv.iter().map(|s| s.to_string()).collect(),
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum RunCmdError {
|
|
#[error("failed to write response to stdout: {0:?}")]
|
|
WriteResponse(Response, #[source] MessageError),
|
|
|
|
#[error(transparent)]
|
|
RunLog(#[from] RunLogError),
|
|
|
|
#[error("failed to run command {0:?}")]
|
|
Command(Vec<String>, #[source] std::io::Error),
|
|
|
|
#[error("command failed with exit code {0}: {1:?}")]
|
|
CommandFailed(i32, Vec<String>),
|
|
|
|
#[error("failed to make pathname absolute: {0}")]
|
|
Canonicalize(PathBuf, #[source] std::io::Error),
|
|
}
|