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
/// Run a shell command, ex: shell!("grep -R '#deadwiki' {}", dir())
macro_rules! shell {
    ($cmd:expr) => {{
        #[cfg(debug_assertions)]
        println!("sh> {}", $cmd);
        crate::shell::shell("sh", &["-c", $cmd.as_ref()])
    }};
    ($cmd:expr, $($arg:tt)+) => {
        shell!(format!($cmd, $($arg)+));
    };
}

/// Run a script and return its output.
pub fn shell(path: &str, args: &[&str]) -> Result<String, std::io::Error> {
    let output = std::process::Command::new(path).args(args).output()?;
    let out = if output.status.success() {
        output.stdout
    } else {
        output.stderr
    };
    match String::from_utf8(out) {
        Ok(s) => Ok(s),
        Err(e) => Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            e.to_string(),
        )),
    }
}