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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use std::io::{self, BufRead, BufReader};
use std::process::{Command, ExitStatus, Stdio, Output};
use std::thread;



/// Utility methods for [std::process::Command]
pub trait CommandExt {
    /// [Command::status], but returns an error if the process didn't have a zero exit code
    fn status0(&mut self) -> io::Result<()>;

    /// [Command::output], but returns an error if the process didn't have a zero exit code
    fn output0(&mut self) -> io::Result<Output>;

    /// [Command::output], but:
    ///
    /// * Returns an error if the process didn't have a zero exit code
    /// * Returns an error if stdout wasn't valid unicode
    /// * Returns *only* stdout
    /// * Stderr is inherited instead of redirected
    fn stdout0(&mut self) -> io::Result<String>;

    /// [Command::output], but:
    ///
    /// * Returns an error if the process didn't have a zero exit code
    /// * Returns an error if stdout wasn't valid unicode
    /// * Returns *only* stdout
    /// * Stderr is nulled instead of redirected
    fn stdout0_no_stderr(&mut self) -> io::Result<String>;

    /// [Command::status], but provides a callback for stdout/stderr
    fn io(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<ExitStatus>;

    /// [Command::status], but provides a callback for stdout/stderr and returns an error if the process didn't have a zero exit code
    fn io0(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<()>;
}

impl CommandExt for Command {
    fn status0(&mut self) -> io::Result<()> {
        let status = self.status()?;
        match status.code() {
            Some(0) => Ok(()),
            Some(n) => Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
            None    => Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
        }
    }

    fn output0(&mut self) -> io::Result<Output> {
        let output = self.output()?;
        match output.status.code() {
            Some(0) => Ok(output),
            Some(n) => Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
            None    => Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
        }
    }

    fn stdout0(&mut self) -> io::Result<String> {
        let output = self.stderr(Stdio::inherit()).output()?;
        match output.status.code() {
            Some(0) => {},
            Some(n) => return Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
            None    => return Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
        }
        String::from_utf8(output.stdout).map_err(|_err| io::Error::new(io::ErrorKind::InvalidData, "stdout contained invalid unicode"))
    }

    fn stdout0_no_stderr(&mut self) -> io::Result<String> {
        let output = self.stderr(Stdio::null()).output()?;
        match output.status.code() {
            Some(0) => {},
            Some(n) => return Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
            None    => return Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
        }
        String::from_utf8(output.stdout).map_err(|_err| io::Error::new(io::ErrorKind::InvalidData, "stdout contained invalid unicode"))
    }

    fn io(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<ExitStatus> {
        let mut child = self.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;

        let stdout = child.stdout.take().map(|stdout| thread::spawn(move ||{
            for line in BufReader::new(stdout).lines() {
                on_out(&line.unwrap());
            }
        }));
        let stderr = child.stderr.take().map(|stderr| thread::spawn(move ||{
            for line in BufReader::new(stderr).lines() {
                on_err(&line.unwrap());
            }
        }));
        let es = child.wait()?;
        stdout.map(|t| t.join().unwrap());
        stderr.map(|t| t.join().unwrap());
        Ok(es)
    }

    fn io0(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<()> {
        let status = self.io(on_out, on_err)?;
        match status.code() {
            Some(0) => Ok(()),
            Some(n) => Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
            None    => Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
        }
    }
}