pub record: Vec<HookEntry>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct HookEntry(toml::Value);
#[derive(Debug, Serialize, Deserialize)]
struct RawHook {
command: String,
args: Vec<String>,
}
impl HookEntry {
pub fn run(&self) -> Result<(), anyhow::Error> {
let (proc, s) = match &self.0 {
toml::Value::String(ref s) => {
if s.is_empty() {
return Ok(());
}
(
if cfg!(target_os = "windows") {
std::process::Command::new("cmd")
.args(&["/C", s])
.output()
.expect("failed to execute process")
} else {
std::process::Command::new(
std::env::var("SHELL").unwrap_or("sh".to_string()),
)
.arg("-c")
.arg(s)
.output()
.expect("failed to execute process")
},
s.clone(),
)
}
v => {
let hook = v.clone().try_into::<RawHook>()?;
(
std::process::Command::new(&hook.command)
.args(&hook.args)
.output()
.expect("failed to execute process"),
hook.command,
)
}
};
if !proc.status.success() {
use std::io::Write;
let mut stderr = std::io::stderr();
writeln!(stderr, "Hook {:?} exited with code {:?}", s, proc.status)?;
std::process::exit(proc.status.code().unwrap_or(1))
}
Ok(())
}