compose_rs/command/
down.rs

1use std::time::Duration;
2
3use super::{CatchOutput, ComposeCommandArgs};
4use crate::{ComposeCommand, ComposeError};
5
6pub enum RemoveOptions {
7    Local,
8    All,
9}
10
11pub enum DownArgs {
12    RemoveVolumes,
13    RemoveOrphans,
14    RemoveImages(RemoveOptions),
15    Timeout(Duration),
16}
17
18impl ComposeCommandArgs for DownArgs {
19    fn args(&self) -> Vec<String> {
20        match self {
21            DownArgs::RemoveVolumes => vec!["-v".to_string()],
22            DownArgs::RemoveOrphans => vec!["--remove-orphans".to_string()],
23            DownArgs::RemoveImages(opt) => match opt {
24                RemoveOptions::Local => vec!["--rmi".to_string(), "local".to_string()],
25                RemoveOptions::All => vec!["--rmi".to_string(), "all".to_string()],
26            },
27            DownArgs::Timeout(duration) => {
28                vec!["--timeout".to_string(), duration.as_secs().to_string()]
29            }
30        }
31    }
32}
33
34pub struct DownCommand {
35    command: std::process::Command,
36    args: Vec<DownArgs>,
37}
38
39impl DownCommand {
40    pub fn new(command: std::process::Command) -> Self {
41        Self {
42            command,
43            args: Vec::new(),
44        }
45    }
46
47    pub fn remove_volumes(mut self) -> Self {
48        self.args.push(DownArgs::RemoveVolumes);
49        self
50    }
51
52    pub fn remove_orphans(mut self) -> Self {
53        self.args.push(DownArgs::RemoveOrphans);
54        self
55    }
56
57    pub fn remove_images(mut self, opt: RemoveOptions) -> Self {
58        self.args.push(DownArgs::RemoveImages(opt));
59        self
60    }
61
62    pub fn timeout(mut self, duration: Duration) -> Self {
63        self.args.push(DownArgs::Timeout(duration));
64        self
65    }
66}
67
68impl ComposeCommand<(), DownArgs> for DownCommand {
69    const COMMAND: &'static str = "down";
70
71    fn exec(self) -> Result<(), ComposeError> {
72        let mut command = self.command;
73        command.arg(Self::COMMAND);
74
75        for arg in self.args {
76            command.args(&arg.args());
77        }
78
79        command.output().catch_output()?;
80
81        Ok(())
82    }
83}