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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Docker exec command implementation.
//!
//! This module provides a comprehensive implementation of the `docker exec` command
//! with support for all native options and an extensible architecture for any additional options.
use super::{CommandExecutor, DockerCommand, EnvironmentBuilder};
use crate::error::Result;
use async_trait::async_trait;
use std::path::PathBuf;
/// Docker exec command builder with fluent API
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct ExecCommand {
/// The container to execute the command in
container: String,
/// The command to execute
command: Vec<String>,
/// Command executor for extensibility
pub executor: CommandExecutor,
/// Run in detached mode
detach: bool,
/// Override the key sequence for detaching a container
detach_keys: Option<String>,
/// Environment variables
environment: EnvironmentBuilder,
/// Environment files
env_files: Vec<String>,
/// Keep STDIN open even if not attached
interactive: bool,
/// Give extended privileges to the command
privileged: bool,
/// Allocate a pseudo-TTY
tty: bool,
/// Username or UID (format: "<name|uid>[:<group|gid>]")
user: Option<String>,
/// Working directory inside the container
workdir: Option<PathBuf>,
}
/// Output from docker exec command
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecOutput {
/// Standard output from the command
pub stdout: String,
/// Standard error from the command
pub stderr: String,
/// Exit code of the executed command
pub exit_code: i32,
}
impl ExecOutput {
/// Check if the command executed successfully
#[must_use]
pub fn success(&self) -> bool {
self.exit_code == 0
}
/// Get combined output (stdout + stderr)
#[must_use]
pub fn combined_output(&self) -> String {
if self.stderr.is_empty() {
self.stdout.clone()
} else if self.stdout.is_empty() {
self.stderr.clone()
} else {
format!("{}\n{}", self.stdout, self.stderr)
}
}
/// Check if stdout is empty (ignoring whitespace)
#[must_use]
pub fn stdout_is_empty(&self) -> bool {
self.stdout.trim().is_empty()
}
/// Check if stderr is empty (ignoring whitespace)
#[must_use]
pub fn stderr_is_empty(&self) -> bool {
self.stderr.trim().is_empty()
}
}
impl ExecCommand {
/// Create a new exec command for the specified container and command
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["ls".to_string(), "-la".to_string()]);
/// ```
pub fn new(container: impl Into<String>, command: Vec<String>) -> Self {
Self {
container: container.into(),
command,
executor: CommandExecutor::new(),
detach: false,
detach_keys: None,
environment: EnvironmentBuilder::new(),
env_files: Vec::new(),
interactive: false,
privileged: false,
tty: false,
user: None,
workdir: None,
}
}
/// Run in detached mode (background)
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["sleep".to_string(), "10".to_string()])
/// .detach();
/// ```
#[must_use]
pub fn detach(mut self) -> Self {
self.detach = true;
self
}
/// Override the key sequence for detaching a container
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["bash".to_string()])
/// .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
}
/// Add an environment variable
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["env".to_string()])
/// .env("DEBUG", "1")
/// .env("LOG_LEVEL", "info");
/// ```
#[must_use]
pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.environment = self.environment.var(key, value);
self
}
/// Add multiple environment variables
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
/// use std::collections::HashMap;
///
/// let mut env_vars = HashMap::new();
/// env_vars.insert("DEBUG".to_string(), "1".to_string());
/// env_vars.insert("LOG_LEVEL".to_string(), "info".to_string());
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["env".to_string()])
/// .envs(env_vars);
/// ```
#[must_use]
pub fn envs(mut self, vars: std::collections::HashMap<String, String>) -> Self {
self.environment = self.environment.vars(vars);
self
}
/// Add an environment file
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["env".to_string()])
/// .env_file("/path/to/env.file");
/// ```
#[must_use]
pub fn env_file(mut self, file: impl Into<String>) -> Self {
self.env_files.push(file.into());
self
}
/// Keep STDIN open even if not attached
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["bash".to_string()])
/// .interactive();
/// ```
#[must_use]
pub fn interactive(mut self) -> Self {
self.interactive = true;
self
}
/// Give extended privileges to the command
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["mount".to_string()])
/// .privileged();
/// ```
#[must_use]
pub fn privileged(mut self) -> Self {
self.privileged = true;
self
}
/// Allocate a pseudo-TTY
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["bash".to_string()])
/// .tty();
/// ```
#[must_use]
pub fn tty(mut self) -> Self {
self.tty = true;
self
}
/// Set username or UID (format: "<name|uid>[:<group|gid>]")
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["whoami".to_string()])
/// .user("root");
///
/// let exec_cmd2 = ExecCommand::new("my-container", vec!["id".to_string()])
/// .user("1000:1000");
/// ```
#[must_use]
pub fn user(mut self, user: impl Into<String>) -> Self {
self.user = Some(user.into());
self
}
/// Set working directory inside the container
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["pwd".to_string()])
/// .workdir("/app");
/// ```
#[must_use]
pub fn workdir(mut self, workdir: impl Into<PathBuf>) -> Self {
self.workdir = Some(workdir.into());
self
}
/// Convenience method for interactive TTY mode
///
/// # Examples
///
/// ```
/// use docker_wrapper::ExecCommand;
///
/// let exec_cmd = ExecCommand::new("my-container", vec!["bash".to_string()])
/// .it();
/// ```
#[must_use]
pub fn it(self) -> Self {
self.interactive().tty()
}
}
#[async_trait]
impl DockerCommand for ExecCommand {
type Output = ExecOutput;
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!["exec".to_string()];
// Add flags
if self.detach {
args.push("--detach".to_string());
}
if let Some(ref keys) = self.detach_keys {
args.push("--detach-keys".to_string());
args.push(keys.clone());
}
// Add environment variables
for (key, value) in self.environment.as_map() {
args.push("--env".to_string());
args.push(format!("{key}={value}"));
}
// Add environment files
for env_file in &self.env_files {
args.push("--env-file".to_string());
args.push(env_file.clone());
}
if self.interactive {
args.push("--interactive".to_string());
}
if self.privileged {
args.push("--privileged".to_string());
}
if self.tty {
args.push("--tty".to_string());
}
if let Some(ref user) = self.user {
args.push("--user".to_string());
args.push(user.clone());
}
if let Some(ref workdir) = self.workdir {
args.push("--workdir".to_string());
args.push(workdir.to_string_lossy().to_string());
}
// Add any additional raw arguments
args.extend(self.executor.raw_args.clone());
// Add container
args.push(self.container.clone());
// Add command
args.extend(self.command.clone());
args
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
let output = self.execute_command(args).await?;
Ok(ExecOutput {
stdout: output.stdout,
stderr: output.stderr,
exit_code: output.exit_code,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exec_command_builder() {
let cmd = ExecCommand::new("test-container", vec!["ls".to_string(), "-la".to_string()])
.interactive()
.tty()
.env("DEBUG", "1")
.user("root")
.workdir("/app");
let args = cmd.build_command_args();
assert!(args.contains(&"--interactive".to_string()));
assert!(args.contains(&"--tty".to_string()));
assert!(args.contains(&"--env".to_string()));
assert!(args.contains(&"DEBUG=1".to_string()));
assert!(args.contains(&"--user".to_string()));
assert!(args.contains(&"root".to_string()));
assert!(args.contains(&"--workdir".to_string()));
assert!(args.contains(&"/app".to_string()));
assert!(args.contains(&"test-container".to_string()));
assert!(args.contains(&"ls".to_string()));
assert!(args.contains(&"-la".to_string()));
}
#[test]
fn test_exec_command_detach() {
let cmd = ExecCommand::new(
"test-container",
vec!["sleep".to_string(), "10".to_string()],
)
.detach()
.detach_keys("ctrl-p,ctrl-q");
let args = cmd.build_command_args();
assert!(args.contains(&"--detach".to_string()));
assert!(args.contains(&"--detach-keys".to_string()));
assert!(args.contains(&"ctrl-p,ctrl-q".to_string()));
}
#[test]
fn test_exec_command_privileged() {
let cmd = ExecCommand::new("test-container", vec!["mount".to_string()]).privileged();
let args = cmd.build_command_args();
assert!(args.contains(&"--privileged".to_string()));
}
#[test]
fn test_exec_command_env_file() {
let cmd = ExecCommand::new("test-container", vec!["env".to_string()])
.env_file("/path/to/env.file")
.env_file("/another/env.file");
let args = cmd.build_command_args();
assert!(args.contains(&"--env-file".to_string()));
assert!(args.contains(&"/path/to/env.file".to_string()));
assert!(args.contains(&"/another/env.file".to_string()));
}
#[test]
fn test_it_convenience_method() {
let cmd = ExecCommand::new("test-container", vec!["bash".to_string()]).it();
let args = cmd.build_command_args();
assert!(args.contains(&"--interactive".to_string()));
assert!(args.contains(&"--tty".to_string()));
}
#[test]
fn test_exec_output_helpers() {
let output_success = ExecOutput {
stdout: "Hello World".to_string(),
stderr: String::new(),
exit_code: 0,
};
assert!(output_success.success());
assert!(!output_success.stdout_is_empty());
assert!(output_success.stderr_is_empty());
assert_eq!(output_success.combined_output(), "Hello World");
let output_error = ExecOutput {
stdout: String::new(),
stderr: "Error occurred".to_string(),
exit_code: 1,
};
assert!(!output_error.success());
assert!(output_error.stdout_is_empty());
assert!(!output_error.stderr_is_empty());
assert_eq!(output_error.combined_output(), "Error occurred");
let output_combined = ExecOutput {
stdout: "Output".to_string(),
stderr: "Warning".to_string(),
exit_code: 0,
};
assert_eq!(output_combined.combined_output(), "Output\nWarning");
}
#[test]
fn test_exec_command_extensibility() {
let mut cmd = ExecCommand::new("test-container", vec!["test".to_string()]);
// Test extensibility methods
cmd.flag("--some-flag");
cmd.option("--some-option", "value");
cmd.arg("extra-arg");
let args = cmd.build_command_args();
assert!(args.contains(&"--some-flag".to_string()));
assert!(args.contains(&"--some-option".to_string()));
assert!(args.contains(&"value".to_string()));
assert!(args.contains(&"extra-arg".to_string()));
}
}