File size: 2,320 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{
    path::PathBuf,
    process::{self, Stdio},
};

pub struct Command {
    bin: String,
    args: Vec<String>,
    error_message: String,
    dry_run: bool,
    current_dir: Option<PathBuf>,
}

impl Command {
    pub fn program<S: AsRef<str>>(bin: S) -> Self {
        Self {
            bin: bin.as_ref().to_owned(),
            args: vec![],
            error_message: String::new(),
            dry_run: false,
            current_dir: None,
        }
    }

    pub fn args<S: AsRef<str>, V: AsRef<[S]>>(mut self, args: V) -> Self {
        self.args
            .extend(args.as_ref().iter().map(|s| s.as_ref().to_string()));
        self
    }

    pub fn error_message<S: AsRef<str>>(mut self, message: S) -> Self {
        message.as_ref().clone_into(&mut self.error_message);
        self
    }

    pub fn dry_run(mut self, dry_run: bool) -> Self {
        self.dry_run = dry_run;
        self
    }

    pub fn current_dir(mut self, current_dir: PathBuf) -> Self {
        self.current_dir = Some(current_dir);
        self
    }

    pub fn execute(self) {
        let mut cmd = process::Command::new(self.bin);
        cmd.args(&self.args)
            .stderr(Stdio::inherit())
            .stdout(Stdio::inherit());
        if let Some(current_dir) = self.current_dir {
            cmd.current_dir(&current_dir);
        }
        if self.dry_run {
            println!("{cmd:?}");
            return;
        }
        let status = cmd.status();
        assert!(
            {
                if self.error_message.is_empty() {
                    status.unwrap()
                } else {
                    status.expect(&self.error_message)
                }
            }
            .success()
        );
    }

    pub fn output_string(self) -> String {
        let mut cmd = process::Command::new(self.bin);
        cmd.args(&self.args).stderr(Stdio::inherit());
        if let Some(current_dir) = self.current_dir {
            cmd.current_dir(&current_dir);
        }
        let output = cmd.output();
        String::from_utf8({
            if self.error_message.is_empty() {
                output.unwrap().stdout
            } else {
                output.expect(&self.error_message).stdout
            }
        })
        .expect("Stdout contains non UTF-8 characters")
    }
}