syncable_cli/common/
command_utils.rs

1use crate::error::Result;
2use std::process::{Command, Output};
3
4/// Execute a command safely and return the output
5pub fn execute_command(cmd: &str, args: &[&str]) -> Result<Output> {
6    let output = Command::new(cmd)
7        .args(args)
8        .output()?;
9    
10    Ok(output)
11}
12
13/// Check if a command is available in PATH
14pub fn is_command_available(cmd: &str) -> bool {
15    // Try the command directly first
16    if Command::new(cmd)
17        .arg("--version")
18        .output()
19        .map(|o| o.status.success())
20        .unwrap_or(false) {
21        return true;
22    }
23    
24    // On Windows, also try with .exe extension
25    if cfg!(windows) && !cmd.ends_with(".exe") {
26        let cmd_with_exe = format!("{}.exe", cmd);
27        return Command::new(&cmd_with_exe)
28            .arg("--version")
29            .output()
30            .map(|o| o.status.success())
31            .unwrap_or(false);
32    }
33    
34    false
35}