use anyhow::{bail, Context, Result};
use std::process::Command;
#[macro_export]
macro_rules! runcmd {
($cmd:expr) => (runcmd!($cmd,));
($cmd:expr, $($args:expr),*) => {{
let mut cmd = Command::new($cmd);
$( cmd.arg($args); )*
let status = cmd.status().with_context(|| format!("running {:#?}", cmd))?;
if !status.success() {
Result::Err(anyhow!("{:#?} failed with {}", cmd, status))
} else {
Result::Ok(())
}
}}
}
#[macro_export]
macro_rules! runcmd_output {
($cmd:expr) => (runcmd_output!($cmd,));
($cmd:expr, $($args:expr),*) => {{
let mut cmd = Command::new($cmd);
$( cmd.arg($args); )*
cmd_output(&mut cmd)
}}
}
pub fn cmd_output(cmd: &mut Command) -> Result<String> {
let result = cmd.output().with_context(|| format!("running {cmd:#?}"))?;
if !result.status.success() {
eprint!("{}", String::from_utf8_lossy(&result.stderr));
bail!("{:#?} failed with {}", cmd, result.status);
}
String::from_utf8(result.stdout)
.with_context(|| format!("decoding as UTF-8 output of `{cmd:#?}`"))
}
pub fn set_die_on_sigpipe() -> Result<()> {
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};
unsafe {
sigaction(
Signal::SIGPIPE,
&SigAction::new(SigHandler::SigDfl, SaFlags::empty(), SigSet::empty()),
)
}
.map(|_| ())
.context("resetting SIGPIPE handler")
}