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
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
//! Docker Pull Command Implementation
//!
//! This module provides a comprehensive implementation of the `docker pull` command,
//! supporting all native Docker pull options for downloading images from registries.
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```no_run
//! use docker_wrapper::PullCommand;
//! use docker_wrapper::DockerCommand;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Basic pull of an image
//!     let pull_cmd = PullCommand::new("nginx:latest");
//!     let output = pull_cmd.execute().await?;
//!     println!("Pull completed: {}", output.success);
//!     Ok(())
//! }
//! ```
//!
//! ## Advanced Usage
//!
//! ```no_run
//! use docker_wrapper::PullCommand;
//! use docker_wrapper::DockerCommand;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Pull all tags for a repository
//!     let pull_cmd = PullCommand::new("alpine")
//!         .all_tags()
//!         .platform("linux/amd64")
//!         .quiet();
//!
//!     let output = pull_cmd.execute().await?;
//!     println!("All tags pulled: {}", output.success);
//!     Ok(())
//! }
//! ```

use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;

/// Docker Pull Command Builder
///
/// Implements the `docker pull` command for downloading images from registries.
///
/// # Docker Pull Overview
///
/// The pull command downloads images from Docker registries (like Docker Hub)
/// to the local Docker daemon. It supports:
/// - Single image pull by name and tag
/// - All tags pull for a repository
/// - Multi-platform image selection
/// - Quiet mode for minimal output
/// - Content trust verification control
///
/// # Image Naming
///
/// Images can be specified in several formats:
/// - `image` - Defaults to latest tag
/// - `image:tag` - Specific tag
/// - `image@digest` - Specific digest
/// - `registry/image:tag` - Specific registry
/// - `registry:port/image:tag` - Custom registry port
///
/// # Examples
///
/// ```no_run
/// use docker_wrapper::PullCommand;
/// use docker_wrapper::DockerCommand;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Pull latest nginx
///     let output = PullCommand::new("nginx")
///         .execute()
///         .await?;
///
///     println!("Pull success: {}", output.success);
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct PullCommand {
    /// Image name with optional tag or digest
    image: String,
    /// Download all tagged images in the repository
    all_tags: bool,
    /// Skip image verification (disable content trust)
    disable_content_trust: bool,
    /// Set platform if server is multi-platform capable
    platform: Option<String>,
    /// Suppress verbose output
    quiet: bool,
    /// Command executor for handling raw arguments and execution
    pub executor: CommandExecutor,
}

impl PullCommand {
    /// Create a new `PullCommand` instance
    ///
    /// # Arguments
    ///
    /// * `image` - The image name to pull (e.g., "nginx:latest", "alpine", "redis:7.0")
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx:latest");
    /// ```
    #[must_use]
    pub fn new<S: Into<String>>(image: S) -> Self {
        Self {
            image: image.into(),
            all_tags: false,
            disable_content_trust: false,
            platform: None,
            quiet: false,
            executor: CommandExecutor::new(),
        }
    }

    /// Download all tagged images in the repository
    ///
    /// When enabled, pulls all available tags for the specified image repository.
    /// Cannot be used with specific tags or digests.
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("alpine")
    ///     .all_tags();
    /// ```
    #[must_use]
    pub fn all_tags(mut self) -> Self {
        self.all_tags = true;
        self
    }

    /// Skip image verification (disable content trust)
    ///
    /// By default, Docker may verify image signatures when content trust is enabled.
    /// This option disables that verification.
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx:latest")
    ///     .disable_content_trust();
    /// ```
    #[must_use]
    pub fn disable_content_trust(mut self) -> Self {
        self.disable_content_trust = true;
        self
    }

    /// Set platform if server is multi-platform capable
    ///
    /// Specifies the platform for which to pull the image when the image
    /// supports multiple platforms (architectures).
    ///
    /// Common platform values:
    /// - `linux/amd64` - 64-bit Intel/AMD Linux
    /// - `linux/arm64` - 64-bit ARM Linux
    /// - `linux/arm/v7` - 32-bit ARM Linux
    /// - `windows/amd64` - 64-bit Windows
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx:latest")
    ///     .platform("linux/arm64");
    /// ```
    #[must_use]
    pub fn platform<S: Into<String>>(mut self, platform: S) -> Self {
        self.platform = Some(platform.into());
        self
    }

    /// Suppress verbose output
    ///
    /// Reduces the amount of output during the pull operation.
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx:latest")
    ///     .quiet();
    /// ```
    #[must_use]
    pub fn quiet(mut self) -> Self {
        self.quiet = true;
        self
    }

    /// Get the image name
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx:latest");
    /// assert_eq!(pull_cmd.get_image(), "nginx:latest");
    /// ```
    #[must_use]
    pub fn get_image(&self) -> &str {
        &self.image
    }

    /// Check if all tags mode is enabled
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("alpine").all_tags();
    /// assert!(pull_cmd.is_all_tags());
    /// ```
    #[must_use]
    pub fn is_all_tags(&self) -> bool {
        self.all_tags
    }

    /// Check if quiet mode is enabled
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx").quiet();
    /// assert!(pull_cmd.is_quiet());
    /// ```
    #[must_use]
    pub fn is_quiet(&self) -> bool {
        self.quiet
    }

    /// Get the platform if set
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx").platform("linux/arm64");
    /// assert_eq!(pull_cmd.get_platform(), Some("linux/arm64"));
    /// ```
    #[must_use]
    pub fn get_platform(&self) -> Option<&str> {
        self.platform.as_deref()
    }

    /// Check if content trust is disabled
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PullCommand;
    ///
    /// let pull_cmd = PullCommand::new("nginx").disable_content_trust();
    /// assert!(pull_cmd.is_content_trust_disabled());
    /// ```
    #[must_use]
    pub fn is_content_trust_disabled(&self) -> bool {
        self.disable_content_trust
    }

    /// Get a reference to the command executor
    #[must_use]
    pub fn get_executor(&self) -> &CommandExecutor {
        &self.executor
    }

    /// Get a mutable reference to the command executor
    #[must_use]
    pub fn get_executor_mut(&mut self) -> &mut CommandExecutor {
        &mut self.executor
    }
}

impl Default for PullCommand {
    fn default() -> Self {
        Self::new("hello-world")
    }
}

#[async_trait]
impl DockerCommand for PullCommand {
    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!["pull".to_string()];

        // Add all-tags flag
        if self.all_tags {
            args.push("--all-tags".to_string());
        }

        // Add disable-content-trust flag
        if self.disable_content_trust {
            args.push("--disable-content-trust".to_string());
        }

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

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

        // Add image name (must be last)
        args.push(self.image.clone());

        // Add raw args from executor
        args.extend(self.executor.raw_args.clone());

        args
    }

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

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

    #[test]
    fn test_pull_command_basic() {
        let pull_cmd = PullCommand::new("nginx:latest");
        let args = pull_cmd.build_command_args();

        assert_eq!(args, vec!["pull", "nginx:latest"]);
        assert_eq!(pull_cmd.get_image(), "nginx:latest");
        assert!(!pull_cmd.is_all_tags());
        assert!(!pull_cmd.is_quiet());
        assert!(!pull_cmd.is_content_trust_disabled());
        assert_eq!(pull_cmd.get_platform(), None);
    }

    #[test]
    fn test_pull_command_with_all_tags() {
        let pull_cmd = PullCommand::new("alpine").all_tags();
        let args = pull_cmd.build_command_args();

        assert!(args.contains(&"--all-tags".to_string()));
        assert!(args.contains(&"alpine".to_string()));
        assert_eq!(args[0], "pull");
        assert!(pull_cmd.is_all_tags());
    }

    #[test]
    fn test_pull_command_with_platform() {
        let pull_cmd = PullCommand::new("nginx:latest").platform("linux/arm64");
        let args = pull_cmd.build_command_args();

        assert!(args.contains(&"--platform".to_string()));
        assert!(args.contains(&"linux/arm64".to_string()));
        assert!(args.contains(&"nginx:latest".to_string()));
        assert_eq!(args[0], "pull");
        assert_eq!(pull_cmd.get_platform(), Some("linux/arm64"));
    }

    #[test]
    fn test_pull_command_with_quiet() {
        let pull_cmd = PullCommand::new("redis:7.0").quiet();
        let args = pull_cmd.build_command_args();

        assert!(args.contains(&"--quiet".to_string()));
        assert!(args.contains(&"redis:7.0".to_string()));
        assert_eq!(args[0], "pull");
        assert!(pull_cmd.is_quiet());
    }

    #[test]
    fn test_pull_command_disable_content_trust() {
        let pull_cmd = PullCommand::new("ubuntu:22.04").disable_content_trust();
        let args = pull_cmd.build_command_args();

        assert!(args.contains(&"--disable-content-trust".to_string()));
        assert!(args.contains(&"ubuntu:22.04".to_string()));
        assert_eq!(args[0], "pull");
        assert!(pull_cmd.is_content_trust_disabled());
    }

    #[test]
    fn test_pull_command_all_options() {
        let pull_cmd = PullCommand::new("postgres")
            .all_tags()
            .platform("linux/amd64")
            .quiet()
            .disable_content_trust();

        let args = pull_cmd.build_command_args();

        assert!(args.contains(&"--all-tags".to_string()));
        assert!(args.contains(&"--platform".to_string()));
        assert!(args.contains(&"linux/amd64".to_string()));
        assert!(args.contains(&"--quiet".to_string()));
        assert!(args.contains(&"--disable-content-trust".to_string()));
        assert!(args.contains(&"postgres".to_string()));
        assert_eq!(args[0], "pull");

        // Verify helper methods
        assert!(pull_cmd.is_all_tags());
        assert!(pull_cmd.is_quiet());
        assert!(pull_cmd.is_content_trust_disabled());
        assert_eq!(pull_cmd.get_platform(), Some("linux/amd64"));
        assert_eq!(pull_cmd.get_image(), "postgres");
    }

    #[test]
    fn test_pull_command_with_registry() {
        let pull_cmd = PullCommand::new("registry.hub.docker.com/library/nginx:alpine");
        let args = pull_cmd.build_command_args();

        assert_eq!(
            args,
            vec!["pull", "registry.hub.docker.com/library/nginx:alpine"]
        );
        assert_eq!(
            pull_cmd.get_image(),
            "registry.hub.docker.com/library/nginx:alpine"
        );
    }

    #[test]
    fn test_pull_command_with_digest() {
        let pull_cmd = PullCommand::new(
            "nginx@sha256:abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab",
        );
        let args = pull_cmd.build_command_args();

        assert_eq!(
            args,
            vec![
                "pull",
                "nginx@sha256:abcd1234567890abcdef1234567890abcdef1234567890abcdef1234567890ab"
            ]
        );
    }

    #[test]
    fn test_pull_command_order() {
        let pull_cmd = PullCommand::new("alpine:3.18")
            .quiet()
            .platform("linux/arm64")
            .all_tags();

        let args = pull_cmd.build_command_args();

        // Command should be first
        assert_eq!(args[0], "pull");

        // Image should be last
        assert_eq!(args.last(), Some(&"alpine:3.18".to_string()));

        // All options should be present
        assert!(args.contains(&"--all-tags".to_string()));
        assert!(args.contains(&"--platform".to_string()));
        assert!(args.contains(&"linux/arm64".to_string()));
        assert!(args.contains(&"--quiet".to_string()));
    }

    #[test]
    fn test_pull_command_default() {
        let pull_cmd = PullCommand::default();
        assert_eq!(pull_cmd.get_image(), "hello-world");
    }
}