use anyhow::Result;
use std::process::{Command, Stdio};
pub fn run_and_capture(program: &str, args: &[String]) -> Result<CaptureResult> {
let output = Command::new(program)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()?;
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
let mut combined = stdout;
if !stderr.is_empty() {
if !combined.is_empty() && !combined.ends_with('\n') {
combined.push('\n');
}
combined.push_str(&stderr);
}
Ok(CaptureResult {
combined,
exit_code: output.status.code(),
})
}
pub struct CaptureResult {
pub combined: String,
pub exit_code: Option<i32>,
}