use std::io::Write;
use std::process::{Command, Stdio};
pub fn binary() -> &'static str {
env!("CARGO_BIN_EXE_safe-chains")
}
pub fn bare_home() -> &'static std::path::Path {
use std::sync::OnceLock;
static HOME: OnceLock<tempfile::TempDir> = OnceLock::new();
HOME.get_or_init(|| tempfile::tempdir().expect("tempdir")).path()
}
pub fn run_hook(args: &[&str], stdin_payload: &str) -> (String, String, i32) {
let mut child = Command::new(binary())
.args(args)
.env("HOME", bare_home())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn safe-chains");
match child.stdin.as_mut().expect("stdin was piped").write_all(stdin_payload.as_bytes()) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => {}
Err(e) => panic!("write to safe-chains stdin failed: {e}"),
}
let out = child.wait_with_output().expect("wait");
(String::from_utf8_lossy(&out.stdout).into_owned(), String::from_utf8_lossy(&out.stderr).into_owned(), out.status.code().unwrap_or(-1))
}