Skip to main content

ai_agent/utils/
prompt_shell_execution.rs

1//! Prompt shell execution utilities.
2
3use std::process::Command;
4
5/// Execute a shell command and return the result
6pub async fn execute_prompt_shell(command: &str) -> Result<String, String> {
7    let output = Command::new("sh")
8        .args(["-c", command])
9        .output()
10        .map_err(|e| e.to_string())?;
11
12    if output.status.success() {
13        Ok(String::from_utf8_lossy(&output.stdout).to_string())
14    } else {
15        Err(String::from_utf8_lossy(&output.stderr).to_string())
16    }
17}
18
19/// Build a shell command with proper escaping
20pub fn build_shell_command(program: &str, args: &[&str]) -> String {
21    let mut cmd = program.to_string();
22
23    for arg in args {
24        cmd.push(' ');
25        cmd.push_str(&shell_escape(arg));
26    }
27
28    cmd
29}
30
31/// Escape a string for shell usage
32fn shell_escape(s: &str) -> String {
33    if s.chars()
34        .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '.')
35    {
36        s.to_string()
37    } else {
38        format!("'{}'", s.replace('\'', "'\\''"))
39    }
40}