docker_wrapper/command/
pause.rs1use super::{CommandExecutor, CommandOutput, DockerCommand};
6use crate::error::Result;
7use async_trait::async_trait;
8
9#[derive(Debug, Clone)]
32pub struct PauseCommand {
33 containers: Vec<String>,
35 pub executor: CommandExecutor,
37}
38
39impl PauseCommand {
40 #[must_use]
50 pub fn new(container: impl Into<String>) -> Self {
51 Self {
52 containers: vec![container.into()],
53 executor: CommandExecutor::new(),
54 }
55 }
56
57 #[must_use]
67 pub fn new_multiple(containers: Vec<impl Into<String>>) -> Self {
68 Self {
69 containers: containers.into_iter().map(Into::into).collect(),
70 executor: CommandExecutor::new(),
71 }
72 }
73
74 #[must_use]
76 pub fn container(mut self, container: impl Into<String>) -> Self {
77 self.containers.push(container.into());
78 self
79 }
80
81 pub async fn run(&self) -> Result<PauseResult> {
106 let output = self.execute().await?;
107 Ok(PauseResult {
108 output,
109 containers: self.containers.clone(),
110 })
111 }
112}
113
114#[async_trait]
115impl DockerCommand for PauseCommand {
116 type Output = CommandOutput;
117
118 fn get_executor(&self) -> &CommandExecutor {
119 &self.executor
120 }
121
122 fn get_executor_mut(&mut self) -> &mut CommandExecutor {
123 &mut self.executor
124 }
125
126 fn build_command_args(&self) -> Vec<String> {
127 let mut args = vec!["pause".to_string()];
128 args.extend(self.containers.clone());
129 args.extend(self.executor.raw_args.clone());
130 args
131 }
132
133 async fn execute(&self) -> Result<Self::Output> {
134 if self.containers.is_empty() {
135 return Err(crate::error::Error::invalid_config(
136 "No containers specified for pausing",
137 ));
138 }
139
140 let args = self.build_command_args();
141 let command_name = args[0].clone();
142 let command_args = args[1..].to_vec();
143 self.executor
144 .execute_command(&command_name, command_args)
145 .await
146 }
147}
148
149#[derive(Debug, Clone)]
151pub struct PauseResult {
152 pub output: CommandOutput,
154 pub containers: Vec<String>,
156}
157
158impl PauseResult {
159 #[must_use]
161 pub fn success(&self) -> bool {
162 self.output.success
163 }
164
165 #[must_use]
167 pub fn paused_containers(&self) -> &[String] {
168 &self.containers
169 }
170
171 #[must_use]
173 pub fn paused_count(&self) -> usize {
174 self.containers.len()
175 }
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 #[test]
183 fn test_pause_single_container() {
184 let cmd = PauseCommand::new("test-container");
185 let args = cmd.build_command_args();
186 assert_eq!(args, vec!["pause", "test-container"]);
187 }
188
189 #[test]
190 fn test_pause_multiple_containers() {
191 let cmd = PauseCommand::new_multiple(vec!["web", "db", "cache"]);
192 let args = cmd.build_command_args();
193 assert_eq!(args, vec!["pause", "web", "db", "cache"]);
194 }
195
196 #[test]
197 fn test_pause_add_container() {
198 let cmd = PauseCommand::new("web").container("db").container("cache");
199 let args = cmd.build_command_args();
200 assert_eq!(args, vec!["pause", "web", "db", "cache"]);
201 }
202}