mho 0.1.0

A Rust port of ShellCrash - A tool for managing proxy cores like Clash/Mihomo/SingBox
// Cross-platform command execution utilities

use crate::{
    error::{MhoError, Result},
    log_info,
};
use std::process::{Command, Stdio};

/// Execute a command synchronously and return its output
pub fn execute(cmd: &str, args: &[&str]) -> Result<String> {
    log_info!("execute {} {}", cmd, args.join(" "));
    let mut c = Command::new(cmd);
    c.args(args);

    #[cfg(target_os = "windows")]
    {
        use std::os::windows::process::CommandExt;
        const CREATE_NO_WINDOW: u32 = 0x08000000;
        c.creation_flags(CREATE_NO_WINDOW);
    }

    c.stdin(Stdio::null())
        .stderr(Stdio::piped())
        .stdout(Stdio::piped());

    let output = c
        .output()
        .map_err(|e| MhoError::Platform(format!("Failed to execute command '{}': {}", cmd, e)))?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    } else {
        let stderr = String::from_utf8_lossy(&output.stderr);
        Err(MhoError::Platform(format!(
            "Command '{}' failed with status {}: {}",
            cmd, output.status, stderr
        )))
    }
}