flv_util/
cmd.rs

1use std::io;
2use std::io::Write;
3use std::process::{Command, ExitStatus};
4use tracing::debug;
5
6pub trait CommandExt {
7    fn inherit(&mut self);
8
9    // wait and check
10    fn wait_and_check(&mut self);
11
12    // just wait
13    fn wait(&mut self);
14
15    fn print(&mut self) -> &mut Self;
16
17    fn rust_log(&mut self, log_option: Option<&str>) -> &mut Self;
18}
19
20impl CommandExt for Command {
21    /// execute and ensure command has been executed ok
22    fn inherit(&mut self) {
23        use std::process::Stdio;
24
25        self.print();
26
27        let output = self
28            .stdout(Stdio::inherit())
29            .output()
30            .expect("execution failed");
31
32        if !output.status.success() {
33            io::stderr().write_all(&output.stderr).unwrap();
34        }
35
36        output.status.check();
37    }
38
39    /// execute and ensure command has been executed ok
40    fn wait_and_check(&mut self) {
41        self.print();
42
43        let output = self.output().expect("execution failed");
44
45        io::stdout().write_all(&output.stdout).unwrap();
46        io::stderr().write_all(&output.stderr).unwrap();
47
48        output.status.check();
49    }
50
51    /// execute and wait, ignore error
52    fn wait(&mut self) {
53        self.print();
54        let output = self.output().expect("execution failed");
55
56        io::stdout().write_all(&output.stdout).unwrap();
57        io::stderr().write_all(&output.stderr).unwrap();
58    }
59
60    fn print(&mut self) -> &mut Self {
61        use std::env;
62
63        debug!("> {}", format!("{:?}", self).replace("\"", ""));
64        if env::var_os("FLV_CMD").is_some() {
65            println!(">> {}", format!("{:?}", self).replace("\"", ""));
66        }
67
68        self
69    }
70
71    fn rust_log(&mut self, log_option: Option<&str>) -> &mut Self {
72        if let Some(log) = log_option {
73            println!("setting rust log: {}", log);
74            self.env("RUST_LOG", log);
75        }
76
77        self
78    }
79}
80
81trait StatusExt {
82    fn check(&self);
83}
84
85impl StatusExt for ExitStatus {
86    fn check(&self) {
87        if !self.success() {
88            match self.code() {
89                Some(code) => println!("Exited with status code: {}", code),
90                None => println!("Process terminated by signal"),
91            }
92            unreachable!()
93        }
94    }
95}