compose_rs/command/
scale.rs

1use super::{CatchOutput, ComposeCommandArgs};
2use crate::{ComposeCommand, ComposeError};
3
4pub enum ScaleArgs {
5    NoDeps,
6    Service(u32, String),
7}
8
9impl ComposeCommandArgs for ScaleArgs {
10    fn args(&self) -> Vec<String> {
11        match self {
12            ScaleArgs::NoDeps => vec!["--no-deps".to_string()],
13            ScaleArgs::Service(count, service) => {
14                vec![format!("{}={}", service, count)]
15            }
16        }
17    }
18}
19pub struct ScaleCommand {
20    command: std::process::Command,
21    args: Vec<ScaleArgs>,
22}
23
24impl ScaleCommand {
25    pub fn new(command: std::process::Command) -> Self {
26        Self {
27            command,
28            args: Vec::new(),
29        }
30    }
31
32    pub fn no_deps(mut self) -> Self {
33        self.args.push(ScaleArgs::NoDeps);
34        self
35    }
36
37    pub fn service(mut self, count: u32, service: &str) -> Self {
38        self.args
39            .push(ScaleArgs::Service(count, service.to_string()));
40        self
41    }
42}
43
44impl ComposeCommand<(), ScaleArgs> for ScaleCommand {
45    const COMMAND: &'static str = "scale";
46
47    fn exec(self) -> Result<(), ComposeError> {
48        let mut command = self.command;
49        command.arg(Self::COMMAND);
50
51        // first use no_deps if it is present
52        if let Some(no_deps) = self.args.iter().find(|a| matches!(a, ScaleArgs::NoDeps)) {
53            command.args(&no_deps.args());
54        }
55
56        // then apply all service args
57        let scale_args = self
58            .args
59            .iter()
60            .filter(|a| !matches!(a, ScaleArgs::NoDeps))
61            .collect::<Vec<&ScaleArgs>>();
62
63        if scale_args.is_empty() {
64            return Err(ComposeError::InvalidArguments(
65                "No service specified".to_string(),
66            ));
67        }
68
69        for arg in scale_args {
70            command.args(&arg.args());
71        }
72
73        command.output().catch_output()?;
74
75        Ok(())
76    }
77}