docker-wrapper 0.11.1

A Docker CLI wrapper for Rust
Documentation
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
//! Docker Compose config command implementation using unified trait pattern.

use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;

/// Docker Compose config command builder
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)] // Multiple boolean flags are appropriate for config command
pub struct ComposeConfigCommand {
    /// Base command executor
    pub executor: CommandExecutor,
    /// Base compose configuration
    pub config: ComposeConfig,
    /// Output format
    pub format: Option<ConfigFormat>,
    /// Resolve image digests
    pub resolve_image_digests: bool,
    /// Don't interpolate environment variables
    pub no_interpolate: bool,
    /// Don't normalize paths
    pub no_normalize: bool,
    /// Don't check consistency
    pub no_consistency: bool,
    /// Show services only
    pub services: bool,
    /// Show volumes only
    pub volumes: bool,
    /// Show profiles only
    pub profiles: bool,
    /// Show images only
    pub images: bool,
    /// Hash of services to include
    pub hash: Option<String>,
    /// Output file path
    pub output: Option<String>,
    /// Quiet mode
    pub quiet: bool,
}

/// Config output format
#[derive(Debug, Clone, Copy)]
pub enum ConfigFormat {
    /// YAML format (default)
    Yaml,
    /// JSON format
    Json,
}

impl std::fmt::Display for ConfigFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Yaml => write!(f, "yaml"),
            Self::Json => write!(f, "json"),
        }
    }
}

/// Result from compose config command
#[derive(Debug, Clone)]
pub struct ComposeConfigResult {
    /// Raw stdout output (configuration YAML/JSON)
    pub stdout: String,
    /// Raw stderr output
    pub stderr: String,
    /// Success status
    pub success: bool,
    /// Whether configuration is valid
    pub is_valid: bool,
}

impl ComposeConfigCommand {
    /// Create a new compose config command
    #[must_use]
    pub fn new() -> Self {
        Self {
            executor: CommandExecutor::new(),
            config: ComposeConfig::new(),
            format: None,
            resolve_image_digests: false,
            no_interpolate: false,
            no_normalize: false,
            no_consistency: false,
            services: false,
            volumes: false,
            profiles: false,
            images: false,
            hash: None,
            output: None,
            quiet: false,
        }
    }

    /// Set output format
    #[must_use]
    pub fn format(mut self, format: ConfigFormat) -> Self {
        self.format = Some(format);
        self
    }

    /// Set output format to JSON
    #[must_use]
    pub fn format_json(mut self) -> Self {
        self.format = Some(ConfigFormat::Json);
        self
    }

    /// Set output format to YAML
    #[must_use]
    pub fn format_yaml(mut self) -> Self {
        self.format = Some(ConfigFormat::Yaml);
        self
    }

    /// Resolve image digests
    #[must_use]
    pub fn resolve_image_digests(mut self) -> Self {
        self.resolve_image_digests = true;
        self
    }

    /// Don't interpolate environment variables
    #[must_use]
    pub fn no_interpolate(mut self) -> Self {
        self.no_interpolate = true;
        self
    }

    /// Don't normalize paths
    #[must_use]
    pub fn no_normalize(mut self) -> Self {
        self.no_normalize = true;
        self
    }

    /// Don't check consistency
    #[must_use]
    pub fn no_consistency(mut self) -> Self {
        self.no_consistency = true;
        self
    }

    /// Show services only
    #[must_use]
    pub fn services(mut self) -> Self {
        self.services = true;
        self
    }

    /// Show volumes only
    #[must_use]
    pub fn volumes(mut self) -> Self {
        self.volumes = true;
        self
    }

    /// Show profiles only
    #[must_use]
    pub fn profiles(mut self) -> Self {
        self.profiles = true;
        self
    }

    /// Show images only
    #[must_use]
    pub fn images(mut self) -> Self {
        self.images = true;
        self
    }

    /// Set hash of services to include
    #[must_use]
    pub fn hash(mut self, hash: impl Into<String>) -> Self {
        self.hash = Some(hash.into());
        self
    }

    /// Set output file path
    #[must_use]
    pub fn output(mut self, output: impl Into<String>) -> Self {
        self.output = Some(output.into());
        self
    }

    /// Enable quiet mode
    #[must_use]
    pub fn quiet(mut self) -> Self {
        self.quiet = true;
        self
    }
}

impl Default for ComposeConfigCommand {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl DockerCommand for ComposeConfigCommand {
    type Output = ComposeConfigResult;

    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> {
        // Use the ComposeCommand implementation explicitly
        <Self as ComposeCommand>::build_command_args(self)
    }

    async fn execute(&self) -> Result<Self::Output> {
        let args = <Self as ComposeCommand>::build_command_args(self);
        let output = self.execute_command(args).await?;

        Ok(ComposeConfigResult {
            stdout: output.stdout.clone(),
            stderr: output.stderr,
            success: output.success,
            is_valid: output.success && !output.stdout.is_empty(),
        })
    }
}

impl ComposeCommand for ComposeConfigCommand {
    fn get_config(&self) -> &ComposeConfig {
        &self.config
    }

    fn get_config_mut(&mut self) -> &mut ComposeConfig {
        &mut self.config
    }

    fn subcommand(&self) -> &'static str {
        "config"
    }

    fn build_subcommand_args(&self) -> Vec<String> {
        let mut args = Vec::new();

        if let Some(format) = self.format {
            args.push("--format".to_string());
            args.push(format.to_string());
        }

        if self.resolve_image_digests {
            args.push("--resolve-image-digests".to_string());
        }

        if self.no_interpolate {
            args.push("--no-interpolate".to_string());
        }

        if self.no_normalize {
            args.push("--no-normalize".to_string());
        }

        if self.no_consistency {
            args.push("--no-consistency".to_string());
        }

        if self.services {
            args.push("--services".to_string());
        }

        if self.volumes {
            args.push("--volumes".to_string());
        }

        if self.profiles {
            args.push("--profiles".to_string());
        }

        if self.images {
            args.push("--images".to_string());
        }

        if let Some(ref hash) = self.hash {
            args.push("--hash".to_string());
            args.push(hash.clone());
        }

        if let Some(ref output) = self.output {
            args.push("--output".to_string());
            args.push(output.clone());
        }

        if self.quiet {
            args.push("--quiet".to_string());
        }

        args
    }
}

impl ComposeConfigResult {
    /// Check if the command was successful
    #[must_use]
    pub fn success(&self) -> bool {
        self.success
    }

    /// Check if the configuration is valid
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.is_valid
    }

    /// Get the configuration output
    #[must_use]
    pub fn config_output(&self) -> &str {
        &self.stdout
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compose_config_basic() {
        let cmd = ComposeConfigCommand::new();
        let args = cmd.build_subcommand_args();
        assert!(args.is_empty());

        let full_args = ComposeCommand::build_command_args(&cmd);
        assert_eq!(full_args[0], "compose");
        assert!(full_args.contains(&"config".to_string()));
    }

    #[test]
    fn test_compose_config_with_format() {
        let cmd = ComposeConfigCommand::new().format_json();
        let args = cmd.build_subcommand_args();
        assert!(args.contains(&"--format".to_string()));
        assert!(args.contains(&"json".to_string()));
    }

    #[test]
    fn test_compose_config_with_flags() {
        let cmd = ComposeConfigCommand::new()
            .resolve_image_digests()
            .no_interpolate()
            .services()
            .quiet();

        let args = cmd.build_subcommand_args();
        assert!(args.contains(&"--resolve-image-digests".to_string()));
        assert!(args.contains(&"--no-interpolate".to_string()));
        assert!(args.contains(&"--services".to_string()));
        assert!(args.contains(&"--quiet".to_string()));
    }

    #[test]
    fn test_compose_config_show_options() {
        let cmd = ComposeConfigCommand::new().volumes().profiles().images();

        let args = cmd.build_subcommand_args();
        assert!(args.contains(&"--volumes".to_string()));
        assert!(args.contains(&"--profiles".to_string()));
        assert!(args.contains(&"--images".to_string()));
    }

    #[test]
    fn test_compose_config_with_hash_and_output() {
        let cmd = ComposeConfigCommand::new()
            .hash("web=sha256:123")
            .output("output.yml");

        let args = cmd.build_subcommand_args();
        assert!(args.contains(&"--hash".to_string()));
        assert!(args.contains(&"web=sha256:123".to_string()));
        assert!(args.contains(&"--output".to_string()));
        assert!(args.contains(&"output.yml".to_string()));
    }

    #[test]
    fn test_config_format_display() {
        assert_eq!(ConfigFormat::Yaml.to_string(), "yaml");
        assert_eq!(ConfigFormat::Json.to_string(), "json");
    }

    #[test]
    fn test_compose_config_integration() {
        let cmd = ComposeConfigCommand::new()
            .file("docker-compose.yml")
            .project_name("myapp")
            .format_json()
            .services();

        let args = ComposeCommand::build_command_args(&cmd);
        assert!(args.contains(&"--file".to_string()));
        assert!(args.contains(&"docker-compose.yml".to_string()));
        assert!(args.contains(&"--project-name".to_string()));
        assert!(args.contains(&"myapp".to_string()));
        assert!(args.contains(&"--format".to_string()));
        assert!(args.contains(&"json".to_string()));
        assert!(args.contains(&"--services".to_string()));
    }
}