use crate::errors::{RalphError, Result};
use std::path::Path;
use tokio::process::Command;
pub async fn run_command(cmd: &str, cwd: &Path) -> Result<String> {
let output = Command::new("sh")
.arg("-c")
.arg(cmd)
.current_dir(cwd)
.output()
.await
.map_err(|e| RalphError::ToolFailed {
tool: "run_command".to_string(),
message: e.to_string(),
})?;
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
let exit_code = output.status.code().unwrap_or(-1);
let mut result = String::new();
if !stdout.is_empty() {
result.push_str(&stdout);
}
if !stderr.is_empty() {
if !result.is_empty() {
result.push('\n');
}
result.push_str(&stderr);
}
result.push_str(&format!("\n[exit: {}]", exit_code));
Ok(result)
}