support_kit/containers/
ops_process.rs

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
use crate::OpsProcessError;

#[derive(Clone, Debug)]
pub struct OpsProcess {
    pub command: String,
    pub args: Vec<String>,
}

impl OpsProcess {
    pub fn run(&self) -> Result<(), OpsProcessError> {
        std::process::Command::new(&self.command)
            .args(&self.args)
            .spawn()?
            .wait()?;

        Ok(())
    }

    pub fn command(&self) -> Vec<String> {
        let mut command = vec![self.command.clone()];
        command.extend(self.args.clone());
        command
    }
}

impl TryFrom<String> for OpsProcess {
    type Error = OpsProcessError;

    fn try_from(command: String) -> Result<Self, Self::Error> {
        let command: &str = command.as_ref();
        command.try_into()
    }
}

impl TryFrom<&str> for OpsProcess {
    type Error = OpsProcessError;

    fn try_from(command: &str) -> Result<Self, Self::Error> {
        let command_chunks: Vec<String> = command.split_whitespace().map(String::from).collect();

        if let Some((command, args)) = command_chunks.split_first() {
            Ok(Self {
                command: command.clone(),
                args: args.to_vec(),
            })
        } else {
            Err(OpsProcessError::MalformedCommand(command.to_string()))
        }
    }
}