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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Docker pause command implementation.
//!
//! This module provides the `docker pause` command for pausing all processes within containers.
use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
/// Docker pause command builder
///
/// Pause all processes within one or more containers.
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::PauseCommand;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Pause a single container
/// PauseCommand::new("my-container")
/// .run()
/// .await?;
///
/// // Pause multiple containers
/// PauseCommand::new_multiple(vec!["web", "db", "cache"])
/// .run()
/// .await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct PauseCommand {
/// Container names or IDs to pause
containers: Vec<String>,
/// Command executor
pub executor: CommandExecutor,
}
impl PauseCommand {
/// Create a new pause command for a single container
///
/// # Example
///
/// ```
/// use docker_wrapper::PauseCommand;
///
/// let cmd = PauseCommand::new("my-container");
/// ```
#[must_use]
pub fn new(container: impl Into<String>) -> Self {
Self {
containers: vec![container.into()],
executor: CommandExecutor::new(),
}
}
/// Create a new pause command for multiple containers
///
/// # Example
///
/// ```
/// use docker_wrapper::PauseCommand;
///
/// let cmd = PauseCommand::new_multiple(vec!["web", "db", "cache"]);
/// ```
#[must_use]
pub fn new_multiple(containers: Vec<impl Into<String>>) -> Self {
Self {
containers: containers.into_iter().map(Into::into).collect(),
executor: CommandExecutor::new(),
}
}
/// Add another container to pause
#[must_use]
pub fn container(mut self, container: impl Into<String>) -> Self {
self.containers.push(container.into());
self
}
/// Execute the pause command
///
/// # Errors
/// Returns an error if:
/// - The Docker daemon is not running
/// - Any of the specified containers don't exist
/// - Any container is not running
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::PauseCommand;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let result = PauseCommand::new("my-container")
/// .run()
/// .await?;
///
/// if result.success() {
/// println!("Paused {} containers", result.paused_containers().len());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn run(&self) -> Result<PauseResult> {
let output = self.execute().await?;
Ok(PauseResult {
output,
containers: self.containers.clone(),
})
}
}
#[async_trait]
impl DockerCommand for PauseCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["pause".to_string()];
args.extend(self.containers.clone());
args.extend(self.executor.raw_args.clone());
args
}
async fn execute(&self) -> Result<Self::Output> {
if self.containers.is_empty() {
return Err(crate::error::Error::invalid_config(
"No containers specified for pausing",
));
}
let args = self.build_command_args();
let command_name = args[0].clone();
let command_args = args[1..].to_vec();
self.executor
.execute_command(&command_name, command_args)
.await
}
}
/// Result from the pause command
#[derive(Debug, Clone)]
pub struct PauseResult {
/// Raw command output
pub output: CommandOutput,
/// Containers that were paused
pub containers: Vec<String>,
}
impl PauseResult {
/// Check if the pause was successful
#[must_use]
pub fn success(&self) -> bool {
self.output.success
}
/// Get the paused container names
#[must_use]
pub fn paused_containers(&self) -> &[String] {
&self.containers
}
/// Get the count of paused containers
#[must_use]
pub fn paused_count(&self) -> usize {
self.containers.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pause_single_container() {
let cmd = PauseCommand::new("test-container");
let args = cmd.build_command_args();
assert_eq!(args, vec!["pause", "test-container"]);
}
#[test]
fn test_pause_multiple_containers() {
let cmd = PauseCommand::new_multiple(vec!["web", "db", "cache"]);
let args = cmd.build_command_args();
assert_eq!(args, vec!["pause", "web", "db", "cache"]);
}
#[test]
fn test_pause_add_container() {
let cmd = PauseCommand::new("web").container("db").container("cache");
let args = cmd.build_command_args();
assert_eq!(args, vec!["pause", "web", "db", "cache"]);
}
}