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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::io::Result;
use std::process::{Command, Stdio};
pub fn exec(cmd: &str, output: bool) -> Result<String> {
debug!("exec `{}`", cmd);
if output {
let output = Command::new("sh")
.arg("-c")
.arg(cmd)
.env("RUST_BACKTRACE", "1")
.output()?;
if !output.status.success() {
return Err(eother!("exit with non-zero status"));
}
let stdout = std::str::from_utf8(&output.stdout).map_err(|e| einval!(e))?;
return Ok(stdout.to_string());
}
let mut child = Command::new("sh")
.arg("-c")
.arg(cmd)
.env("RUST_BACKTRACE", "1")
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;
let status = child.wait()?;
if !status.success() {
return Err(eother!("exit with non-zero status"));
}
Ok(String::from(""))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exec() {
let val = exec("echo hello", true).unwrap();
assert_eq!(val, "hello\n");
let val = exec("echo hello", false).unwrap();
assert_eq!(val, "");
}
}