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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Docker start command implementation.
//!
//! This module provides a comprehensive implementation of the `docker start` command
//! with support for all native options and an extensible architecture.
use super::{CommandExecutor, DockerCommand};
use crate::error::{Error, Result};
use async_trait::async_trait;
/// Docker start command builder with fluent API
#[derive(Debug, Clone)]
pub struct StartCommand {
/// Command executor for extensibility
pub executor: CommandExecutor,
/// Container IDs or names to start
containers: Vec<String>,
/// Attach STDOUT/STDERR and forward signals
attach: bool,
/// Restore from this checkpoint
checkpoint: Option<String>,
/// Use a custom checkpoint storage directory
checkpoint_dir: Option<String>,
/// Override the key sequence for detaching a container
detach_keys: Option<String>,
/// Attach container's STDIN
interactive: bool,
}
/// Result of a start command execution
#[derive(Debug, Clone, PartialEq)]
pub struct StartResult {
/// Raw stdout from the command
pub stdout: String,
/// Raw stderr from the command
pub stderr: String,
/// Container IDs that were started
pub started_containers: Vec<String>,
}
impl StartCommand {
/// Create a new start command for the specified container(s)
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container");
/// ```
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new_multiple(vec!["container1", "container2"]);
/// ```
pub fn new(container: impl Into<String>) -> Self {
Self {
executor: CommandExecutor::new(),
containers: vec![container.into()],
attach: false,
checkpoint: None,
checkpoint_dir: None,
detach_keys: None,
interactive: false,
}
}
/// Create a new start command for multiple containers
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new_multiple(vec!["container1", "container2"]);
/// ```
pub fn new_multiple<I, S>(containers: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
executor: CommandExecutor::new(),
containers: containers.into_iter().map(Into::into).collect(),
attach: false,
checkpoint: None,
checkpoint_dir: None,
detach_keys: None,
interactive: false,
}
}
/// Attach STDOUT/STDERR and forward signals
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container")
/// .attach();
/// ```
#[must_use]
pub fn attach(mut self) -> Self {
self.attach = true;
self
}
/// Restore from this checkpoint
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container")
/// .checkpoint("checkpoint1");
/// ```
#[must_use]
pub fn checkpoint(mut self, checkpoint: impl Into<String>) -> Self {
self.checkpoint = Some(checkpoint.into());
self
}
/// Use a custom checkpoint storage directory
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container")
/// .checkpoint_dir("/custom/checkpoint/dir");
/// ```
#[must_use]
pub fn checkpoint_dir(mut self, dir: impl Into<String>) -> Self {
self.checkpoint_dir = Some(dir.into());
self
}
/// Override the key sequence for detaching a container
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container")
/// .detach_keys("ctrl-p,ctrl-q");
/// ```
#[must_use]
pub fn detach_keys(mut self, keys: impl Into<String>) -> Self {
self.detach_keys = Some(keys.into());
self
}
/// Attach container's STDIN
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container")
/// .interactive();
/// ```
#[must_use]
pub fn interactive(mut self) -> Self {
self.interactive = true;
self
}
/// Convenience method for interactive + attach mode
///
/// # Examples
///
/// ```
/// use docker_wrapper::StartCommand;
///
/// let cmd = StartCommand::new("my-container")
/// .ai(); // attach + interactive
/// ```
#[must_use]
pub fn ai(self) -> Self {
self.attach().interactive()
}
}
#[async_trait]
impl DockerCommand for StartCommand {
type Output = StartResult;
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!["start".to_string()];
// Add attach option
if self.attach {
args.push("--attach".to_string());
}
// Add checkpoint option
if let Some(checkpoint) = &self.checkpoint {
args.push("--checkpoint".to_string());
args.push(checkpoint.clone());
}
// Add checkpoint-dir option
if let Some(checkpoint_dir) = &self.checkpoint_dir {
args.push("--checkpoint-dir".to_string());
args.push(checkpoint_dir.clone());
}
// Add detach-keys option
if let Some(detach_keys) = &self.detach_keys {
args.push("--detach-keys".to_string());
args.push(detach_keys.clone());
}
// Add interactive option
if self.interactive {
args.push("--interactive".to_string());
}
// Add container names/IDs
args.extend(self.containers.clone());
// Add raw arguments from executor
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"));
}
let args = self.build_command_args();
let output = self.execute_command(args).await?;
// Parse the output to extract started container IDs
let started_containers = if output.stdout.trim().is_empty() {
// If no stdout, assume the containers specified were started
self.containers.clone()
} else {
// Parse container IDs from stdout (each line is a container ID)
output
.stdout
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| line.trim().to_string())
.collect()
};
Ok(StartResult {
stdout: output.stdout,
stderr: output.stderr,
started_containers,
})
}
}
impl StartCommand {
/// Get the command arguments (for testing)
#[must_use]
pub fn args(&self) -> Vec<String> {
self.build_command_args()
}
}
impl StartResult {
/// Check if the command was successful
#[must_use]
pub fn is_success(&self) -> bool {
!self.started_containers.is_empty()
}
/// Get the number of containers that were started
#[must_use]
pub fn container_count(&self) -> usize {
self.started_containers.len()
}
/// Get the first started container ID (useful for single container operations)
#[must_use]
pub fn first_container(&self) -> Option<&String> {
self.started_containers.first()
}
/// Check if a specific container was started
#[must_use]
pub fn contains_container(&self, container: &str) -> bool {
self.started_containers.iter().any(|c| c == container)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_start_command_new() {
let cmd = StartCommand::new("test-container");
assert_eq!(cmd.containers, vec!["test-container"]);
assert!(!cmd.attach);
assert!(cmd.checkpoint.is_none());
assert!(cmd.checkpoint_dir.is_none());
assert!(cmd.detach_keys.is_none());
assert!(!cmd.interactive);
}
#[test]
fn test_start_command_new_multiple() {
let cmd = StartCommand::new_multiple(vec!["container1", "container2"]);
assert_eq!(cmd.containers, vec!["container1", "container2"]);
}
#[test]
fn test_start_command_with_attach() {
let cmd = StartCommand::new("test-container").attach();
assert!(cmd.attach);
}
#[test]
fn test_start_command_with_checkpoint() {
let cmd = StartCommand::new("test-container").checkpoint("checkpoint1");
assert_eq!(cmd.checkpoint, Some("checkpoint1".to_string()));
}
#[test]
fn test_start_command_with_checkpoint_dir() {
let cmd = StartCommand::new("test-container").checkpoint_dir("/custom/dir");
assert_eq!(cmd.checkpoint_dir, Some("/custom/dir".to_string()));
}
#[test]
fn test_start_command_with_detach_keys() {
let cmd = StartCommand::new("test-container").detach_keys("ctrl-p,ctrl-q");
assert_eq!(cmd.detach_keys, Some("ctrl-p,ctrl-q".to_string()));
}
#[test]
fn test_start_command_with_interactive() {
let cmd = StartCommand::new("test-container").interactive();
assert!(cmd.interactive);
}
#[test]
fn test_start_command_ai_convenience() {
let cmd = StartCommand::new("test-container").ai();
assert!(cmd.attach);
assert!(cmd.interactive);
}
#[test]
fn test_start_command_args_basic() {
let cmd = StartCommand::new("test-container");
let args = cmd.args();
assert_eq!(args, vec!["start", "test-container"]);
}
#[test]
fn test_start_command_args_with_options() {
let cmd = StartCommand::new("test-container")
.attach()
.interactive()
.checkpoint("checkpoint1");
let args = cmd.args();
assert_eq!(
args,
vec![
"start",
"--attach",
"--checkpoint",
"checkpoint1",
"--interactive",
"test-container"
]
);
}
#[test]
fn test_start_command_args_multiple_containers() {
let cmd =
StartCommand::new_multiple(vec!["container1", "container2"]).detach_keys("ctrl-c");
let args = cmd.args();
assert_eq!(
args,
vec![
"start",
"--detach-keys",
"ctrl-c",
"container1",
"container2"
]
);
}
#[test]
fn test_start_result_is_success() {
let result = StartResult {
stdout: "container1\n".to_string(),
stderr: String::new(),
started_containers: vec!["container1".to_string()],
};
assert!(result.is_success());
let empty_result = StartResult {
stdout: String::new(),
stderr: String::new(),
started_containers: vec![],
};
assert!(!empty_result.is_success());
}
#[test]
fn test_start_result_container_count() {
let result = StartResult {
stdout: String::new(),
stderr: String::new(),
started_containers: vec!["container1".to_string(), "container2".to_string()],
};
assert_eq!(result.container_count(), 2);
}
#[test]
fn test_start_result_first_container() {
let result = StartResult {
stdout: String::new(),
stderr: String::new(),
started_containers: vec!["container1".to_string(), "container2".to_string()],
};
assert_eq!(result.first_container(), Some(&"container1".to_string()));
let empty_result = StartResult {
stdout: String::new(),
stderr: String::new(),
started_containers: vec![],
};
assert_eq!(empty_result.first_container(), None);
}
#[test]
fn test_start_result_contains_container() {
let result = StartResult {
stdout: String::new(),
stderr: String::new(),
started_containers: vec!["container1".to_string(), "container2".to_string()],
};
assert!(result.contains_container("container1"));
assert!(result.contains_container("container2"));
assert!(!result.contains_container("container3"));
}
#[test]
fn test_command_name() {
let cmd = StartCommand::new("test");
let args = cmd.build_command_args();
assert_eq!(args[0], "start");
}
}