mmrbi/
command_ext.rs

1use std::io::{self, BufRead, BufReader};
2use std::process::{Command, ExitStatus, Stdio, Output};
3use std::thread;
4
5
6
7/// Utility methods for [std::process::Command]
8pub trait CommandExt {
9    /// [Command::status], but returns an error if the process didn't have a zero exit code
10    fn status0(&mut self) -> io::Result<()>;
11
12    /// [Command::output], but returns an error if the process didn't have a zero exit code
13    fn output0(&mut self) -> io::Result<Output>;
14
15    /// [Command::output], but:
16    ///
17    /// * Returns an error if the process didn't have a zero exit code
18    /// * Returns an error if stdout wasn't valid unicode
19    /// * Returns *only* stdout
20    /// * Stderr is inherited instead of redirected
21    fn stdout0(&mut self) -> io::Result<String>;
22
23    /// [Command::output], but:
24    ///
25    /// * Returns an error if the process didn't have a zero exit code
26    /// * Returns an error if stdout wasn't valid unicode
27    /// * Returns *only* stdout
28    /// * Stderr is nulled instead of redirected
29    fn stdout0_no_stderr(&mut self) -> io::Result<String>;
30
31    /// [Command::status], but provides a callback for stdout/stderr
32    fn io(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<ExitStatus>;
33
34    /// [Command::status], but provides a callback for stdout/stderr and returns an error if the process didn't have a zero exit code
35    fn io0(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<()>;
36}
37
38impl CommandExt for Command {
39    fn status0(&mut self) -> io::Result<()> {
40        let status = self.status()?;
41        match status.code() {
42            Some(0) => Ok(()),
43            Some(n) => Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
44            None    => Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
45        }
46    }
47
48    fn output0(&mut self) -> io::Result<Output> {
49        let output = self.output()?;
50        match output.status.code() {
51            Some(0) => Ok(output),
52            Some(n) => Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
53            None    => Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
54        }
55    }
56
57    fn stdout0(&mut self) -> io::Result<String> {
58        let output = self.stderr(Stdio::inherit()).output()?;
59        match output.status.code() {
60            Some(0) => {},
61            Some(n) => return Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
62            None    => return Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
63        }
64        String::from_utf8(output.stdout).map_err(|_err| io::Error::new(io::ErrorKind::InvalidData, "stdout contained invalid unicode"))
65    }
66
67    fn stdout0_no_stderr(&mut self) -> io::Result<String> {
68        let output = self.stderr(Stdio::null()).output()?;
69        match output.status.code() {
70            Some(0) => {},
71            Some(n) => return Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
72            None    => return Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
73        }
74        String::from_utf8(output.stdout).map_err(|_err| io::Error::new(io::ErrorKind::InvalidData, "stdout contained invalid unicode"))
75    }
76
77    fn io(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<ExitStatus> {
78        let mut child = self.stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
79
80        let stdout = child.stdout.take().map(|stdout| thread::spawn(move ||{
81            for line in BufReader::new(stdout).lines() {
82                on_out(&line.unwrap());
83            }
84        }));
85        let stderr = child.stderr.take().map(|stderr| thread::spawn(move ||{
86            for line in BufReader::new(stderr).lines() {
87                on_err(&line.unwrap());
88            }
89        }));
90        let es = child.wait()?;
91        stdout.map(|t| t.join().unwrap());
92        stderr.map(|t| t.join().unwrap());
93        Ok(es)
94    }
95
96    fn io0(&mut self, on_out: impl Fn(&str) + Send + Sync + 'static, on_err: impl Fn(&str) + Send + Sync + 'static) -> io::Result<()> {
97        let status = self.io(on_out, on_err)?;
98        match status.code() {
99            Some(0) => Ok(()),
100            Some(n) => Err(io::Error::new(io::ErrorKind::Other, format!("exit code {}", n))),
101            None    => Err(io::Error::new(io::ErrorKind::Other, "terminated by signal")),
102        }
103    }
104}