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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! Docker rm command implementation.
//!
//! This module provides the `docker rm` command for removing stopped containers.
//!
//! # Example
//!
//! ```rust,no_run
//! use docker_wrapper::{DockerCommand, RmCommand, RunCommand, StopCommand};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create and run a container
//! let output = RunCommand::new("alpine:latest")
//! .name("test-rm")
//! .detach()
//! .cmd(vec!["sleep".to_string(), "10".to_string()])
//! .execute()
//! .await?;
//!
//! // Stop the container first
//! StopCommand::new("test-rm")
//! .execute()
//! .await?;
//!
//! // Remove the stopped container
//! RmCommand::new("test-rm")
//! .execute()
//! .await?;
//!
//! // Force remove a running container (stops and removes)
//! let output2 = RunCommand::new("nginx:alpine")
//! .name("test-force-rm")
//! .detach()
//! .execute()
//! .await?;
//!
//! RmCommand::new("test-force-rm")
//! .force() // Force removal even if running
//! .volumes() // Also remove associated volumes
//! .execute()
//! .await?;
//!
//! // Remove multiple containers
//! RmCommand::new_multiple(vec!["container1", "container2", "container3"])
//! .force()
//! .execute()
//! .await?;
//! # Ok(())
//! # }
//! ```
use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::{Error, Result};
use async_trait::async_trait;
/// Docker rm command builder
#[derive(Debug, Clone)]
pub struct RmCommand {
/// Container names or IDs to remove
containers: Vec<String>,
/// Force removal of running containers
force: bool,
/// Remove anonymous volumes associated with the container
volumes: bool,
/// Remove the specified link
link: bool,
/// Command executor
pub executor: CommandExecutor,
}
impl RmCommand {
/// Create a new rm command for a single container
#[must_use]
pub fn new(container: impl Into<String>) -> Self {
Self {
containers: vec![container.into()],
force: false,
volumes: false,
link: false,
executor: CommandExecutor::new(),
}
}
/// Create a new rm command for multiple containers
#[must_use]
pub fn new_multiple(containers: Vec<impl Into<String>>) -> Self {
Self {
containers: containers.into_iter().map(Into::into).collect(),
force: false,
volumes: false,
link: false,
executor: CommandExecutor::new(),
}
}
/// Add another container to remove
#[must_use]
pub fn container(mut self, container: impl Into<String>) -> Self {
self.containers.push(container.into());
self
}
/// Force removal of running containers (uses SIGKILL)
#[must_use]
pub fn force(mut self) -> Self {
self.force = true;
self
}
/// Remove anonymous volumes associated with the container
#[must_use]
pub fn volumes(mut self) -> Self {
self.volumes = true;
self
}
/// Remove the specified link
#[must_use]
pub fn link(mut self) -> Self {
self.link = true;
self
}
/// Execute the rm command
///
/// # Errors
/// Returns an error if:
/// - No containers are specified
/// - The Docker daemon is not running
/// - Any of the specified containers don't exist
/// - A container is running and force flag is not set
pub async fn run(&self) -> Result<RmResult> {
let output = self.execute().await?;
// Parse removed container IDs from output
let removed: Vec<String> = output
.stdout
.lines()
.filter(|line| !line.is_empty())
.map(String::from)
.collect();
Ok(RmResult { removed, output })
}
}
#[async_trait]
impl DockerCommand for RmCommand {
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!["rm".to_string()];
if self.force {
args.push("--force".to_string());
}
if self.volumes {
args.push("--volumes".to_string());
}
if self.link {
args.push("--link".to_string());
}
// Add container names/IDs
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(Error::invalid_config("No containers specified for removal"));
}
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 rm command
#[derive(Debug, Clone)]
pub struct RmResult {
/// List of removed container IDs
pub removed: Vec<String>,
/// Raw command output
pub output: CommandOutput,
}
impl RmResult {
/// Check if all containers were removed successfully
#[must_use]
pub fn all_removed(&self) -> bool {
self.output.success
}
/// Get the number of containers removed
#[must_use]
pub fn count(&self) -> usize {
self.removed.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rm_single_container() {
let cmd = RmCommand::new("test-container");
let args = cmd.build_command_args();
assert_eq!(args, vec!["rm", "test-container"]);
}
#[test]
fn test_rm_multiple_containers() {
let cmd = RmCommand::new_multiple(vec!["container1", "container2", "container3"]);
let args = cmd.build_command_args();
assert_eq!(args, vec!["rm", "container1", "container2", "container3"]);
}
#[test]
fn test_rm_with_force() {
let cmd = RmCommand::new("test-container").force();
let args = cmd.build_command_args();
assert_eq!(args, vec!["rm", "--force", "test-container"]);
}
#[test]
fn test_rm_with_volumes() {
let cmd = RmCommand::new("test-container").volumes();
let args = cmd.build_command_args();
assert_eq!(args, vec!["rm", "--volumes", "test-container"]);
}
#[test]
fn test_rm_with_all_options() {
let cmd = RmCommand::new("test-container").force().volumes().link();
let args = cmd.build_command_args();
assert_eq!(
args,
vec!["rm", "--force", "--volumes", "--link", "test-container"]
);
}
#[test]
fn test_rm_builder_chain() {
let cmd = RmCommand::new("container1")
.container("container2")
.container("container3")
.force()
.volumes();
let args = cmd.build_command_args();
assert_eq!(
args,
vec![
"rm",
"--force",
"--volumes",
"container1",
"container2",
"container3"
]
);
}
}