1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::process::Command;
fn shell_command(command: &str, args: &[&str]) -> Result<String, String> {
let mut c = Command::new(command);
for arg in args {
c.arg(arg);
}
match c.output() {
Ok(result) => {
if result.status.success() {
let stdout = String::from_utf8_lossy(&result.stdout);
Ok((*stdout).to_owned())
} else {
let stderr = String::from_utf8_lossy(&result.stderr);
Err((*stderr).to_owned())
}
}
Err(_) => Err(format!("Could not execute '{}'. Is it on $PATH?", command)),
}
}
pub fn run_shell_command(command: &str, args: &[&str]) -> Result<(), String> {
match shell_command(command, args) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}