1use std::io::{self, BufRead, BufReader};
2use std::process::{Command, ExitStatus, Stdio, Output};
3use std::thread;
4
5
6
7pub trait CommandExt {
9 fn status0(&mut self) -> io::Result<()>;
11
12 fn output0(&mut self) -> io::Result<Output>;
14
15 fn stdout0(&mut self) -> io::Result<String>;
22
23 fn stdout0_no_stderr(&mut self) -> io::Result<String>;
30
31 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 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}