compose_rs/command/
up.rs

1use crate::{ComposeCommand, ComposeError};
2
3use super::{CatchOutput, ComposeCommandArgs};
4
5pub enum UpArgs {
6    /// Scale a service to a number of containers
7    Scale(String, u32),
8    /// Waits for containers to be running|healthy before returning
9    Wait,
10}
11
12impl ComposeCommandArgs for UpArgs {
13    fn args(&self) -> Vec<String> {
14        match self {
15            UpArgs::Scale(service, count) => {
16                vec!["--scale".to_string(), format!("{}={}", service, count)]
17            }
18            UpArgs::Wait => vec!["--wait".to_string()],
19        }
20    }
21}
22
23pub struct UpCommand {
24    command: std::process::Command,
25    args: Vec<UpArgs>,
26}
27
28impl UpCommand {
29    pub fn new(command: std::process::Command) -> Self {
30        Self {
31            command,
32            args: Vec::new(),
33        }
34    }
35
36    pub fn scale(mut self, service: &str, count: u32) -> Self {
37        self.args.push(UpArgs::Scale(service.to_string(), count));
38        self
39    }
40
41    pub fn wait(mut self) -> Self {
42        self.args.push(UpArgs::Wait);
43        self
44    }
45}
46
47impl ComposeCommand<(), UpArgs> for UpCommand {
48    const COMMAND: &'static str = "up";
49
50    fn exec(self) -> Result<(), ComposeError> {
51        let mut command = self.command;
52        command.arg(Self::COMMAND).arg("-d");
53
54        for arg in self.args {
55            command.args(&arg.args());
56        }
57
58        command.output().catch_output()?;
59
60        Ok(())
61    }
62}