switchdev 0.1.0

A fast CLI to instantly switch between development projects and run their startup commands
use crate::error::SwitchdevError;
use std::process::Command;

pub fn run_project(
    path: &str,
    command: &str,
    dry_run: bool,
    verbose: bool,
) -> Result<(), SwitchdevError> {
    let escaped_path = shell_escape(path);
    let full_command = format!("cd {escaped_path} && {command}");

    if verbose {
        println!("[i] Path: {path}");
        println!("[i] Command: {command}");
    }

    if dry_run {
        println!("[i] Dry run mode");
        println!("[i] Would execute: {full_command}");
        return Ok(());
    }

    let status = Command::new("sh")
        .arg("-c")
        .arg(full_command)
        .status()
        .map_err(|_| SwitchdevError::CommandExecutionFailed)?;

    if status.success() {
        Ok(())
    } else {
        Err(SwitchdevError::CommandExecutionFailed)
    }
}

fn shell_escape(value: &str) -> String {
    format!("'{}'", value.replace('\'', r"'\''"))
}