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