Skip to main content

ai_agent/utils/
exec_file_no_throw.rs

1use std::process::{Command, Output, Stdio};
2
3pub struct ExecResult {
4    pub stdout: String,
5    pub stderr: String,
6    pub code: i32,
7}
8
9pub async fn exec_file_no_throw(command: &str, args: Vec<String>) -> ExecResult {
10    exec_file_no_throw_inner(command, args)
11}
12
13fn exec_file_no_throw_inner(command: &str, args: Vec<String>) -> ExecResult {
14    let result = Command::new(command)
15        .args(&args)
16        .stdout(Stdio::piped())
17        .stderr(Stdio::piped())
18        .output();
19
20    match result {
21        Ok(output) => ExecResult {
22            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
23            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
24            code: output.status.code().unwrap_or(-1),
25        },
26        Err(e) => ExecResult {
27            stdout: String::new(),
28            stderr: e.to_string(),
29            code: -1,
30        },
31    }
32}
33
34pub fn exec_file_no_throw_sync(command: &str, args: Vec<String>) -> ExecResult {
35    exec_file_no_throw_inner(command, args)
36}
37
38pub async fn exec_file_no_throw_with_cwd(
39    command: &str,
40    args: Vec<String>,
41    cwd: &std::path::Path,
42) -> ExecResult {
43    let result = Command::new(command)
44        .args(&args)
45        .current_dir(cwd)
46        .stdout(Stdio::piped())
47        .stderr(Stdio::piped())
48        .output();
49
50    match result {
51        Ok(output) => ExecResult {
52            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
53            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
54            code: output.status.code().unwrap_or(-1),
55        },
56        Err(e) => ExecResult {
57            stdout: String::new(),
58            stderr: e.to_string(),
59            code: -1,
60        },
61    }
62}