//! Centralised helpers for running external commands.

use std::process::{Command, Stdio};

/// Run `program args`, returning trimmed stdout on success, or `None` if the
/// command failed to start or exited non-zero. Stderr is discarded.
pub fn output(program: &str, args: &[&str]) -> Option<String> {
    let out = Command::new(program)
        .args(args)
        .stderr(Stdio::null())
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    Some(String::from_utf8_lossy(&out.stdout).trim().to_string())
}

/// Run `program args` to completion, returning whether it exited successfully.
/// Stderr is discarded.
pub fn run(program: &str, args: &[&str]) -> bool {
    Command::new(program)
        .args(args)
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}
