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
503
504
505
506
507
508
509
510
511
512
//! Docker Push Command Implementation
//!
//! This module provides a comprehensive implementation of the `docker push` command,
//! supporting all native Docker push options for uploading images to registries.
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```no_run
//! use docker_wrapper::PushCommand;
//! use docker_wrapper::DockerCommand;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Basic push of an image
//!     let push_cmd = PushCommand::new("myapp:latest");
//!     let output = push_cmd.execute().await?;
//!     println!("Push completed: {}", output.success);
//!     Ok(())
//! }
//! ```
//!
//! ## Advanced Usage
//!
//! ```no_run
//! use docker_wrapper::PushCommand;
//! use docker_wrapper::DockerCommand;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Push all tags for a repository
//!     let push_cmd = PushCommand::new("myregistry.com/myapp")
//!         .all_tags()
//!         .platform("linux/amd64")
//!         .quiet()
//!         .disable_content_trust();
//!
//!     let output = push_cmd.execute().await?;
//!     println!("All tags pushed: {}", output.success);
//!     Ok(())
//! }
//! ```

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

/// Docker Push Command Builder
///
/// Implements the `docker push` command for uploading images to registries.
///
/// # Docker Push Overview
///
/// The push command uploads images from the local Docker daemon to Docker registries
/// (like Docker Hub, AWS ECR, or private registries). It supports:
/// - Single image push by name and tag
/// - All tags push for a repository
/// - Platform-specific manifest pushing
/// - Quiet mode for minimal output
/// - Content trust signing control
///
/// # Image Naming
///
/// Images can be specified in several formats:
/// - `image:tag` - Image with specific tag
/// - `registry/image:tag` - Specific registry
/// - `registry:port/image:tag` - Custom registry port
/// - `namespace/image:tag` - Namespaced image (e.g., Docker Hub organizations)
///
/// # Registry Authentication
///
/// Push operations typically require authentication. Use `docker login` first
/// or configure registry credentials through Docker configuration files.
///
/// # Examples
///
/// ```no_run
/// use docker_wrapper::PushCommand;
/// use docker_wrapper::DockerCommand;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Push to Docker Hub
///     let output = PushCommand::new("username/myapp:v1.0")
///         .execute()
///         .await?;
///
///     println!("Push success: {}", output.success);
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct PushCommand {
    /// Image name with tag to push
    image: String,
    /// Push all tags of an image to the repository
    all_tags: bool,
    /// Skip image signing (disable content trust)
    disable_content_trust: bool,
    /// Push a platform-specific manifest as a single-platform image
    platform: Option<String>,
    /// Suppress verbose output
    quiet: bool,
    /// Command executor for handling raw arguments and execution
    pub executor: CommandExecutor,
}

impl PushCommand {
    /// Create a new `PushCommand` instance
    ///
    /// # Arguments
    ///
    /// * `image` - The image name to push (e.g., "myapp:latest", "registry.com/myapp:v1.0")
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp: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(),
        }
    }

    /// Push all tags of an image to the repository
    ///
    /// When enabled, pushes all available tags for the specified image repository.
    /// The image name should not include a specific tag when using this option.
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myregistry.com/myapp")
    ///     .all_tags();
    /// ```
    #[must_use]
    pub fn all_tags(mut self) -> Self {
        self.all_tags = true;
        self
    }

    /// Skip image signing (disable content trust)
    ///
    /// By default, Docker may sign images when content trust is enabled.
    /// This option disables that signing process.
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp:latest")
    ///     .disable_content_trust();
    /// ```
    #[must_use]
    pub fn disable_content_trust(mut self) -> Self {
        self.disable_content_trust = true;
        self
    }

    /// Push a platform-specific manifest as a single-platform image
    ///
    /// Pushes only the specified platform variant of a multi-platform image.
    /// The image index won't be pushed, meaning other manifests including
    /// attestations won't be preserved.
    ///
    /// Platform format: `os[/arch[/variant]]`
    ///
    /// 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::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp:latest")
    ///     .platform("linux/amd64");
    /// ```
    #[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 push operation.
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp:latest")
    ///     .quiet();
    /// ```
    #[must_use]
    pub fn quiet(mut self) -> Self {
        self.quiet = true;
        self
    }

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

    /// Check if all tags mode is enabled
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp").all_tags();
    /// assert!(push_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::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp").quiet();
    /// assert!(push_cmd.is_quiet());
    /// ```
    #[must_use]
    pub fn is_quiet(&self) -> bool {
        self.quiet
    }

    /// Get the platform if set
    ///
    /// # Examples
    ///
    /// ```
    /// use docker_wrapper::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp").platform("linux/arm64");
    /// assert_eq!(push_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::PushCommand;
    ///
    /// let push_cmd = PushCommand::new("myapp").disable_content_trust();
    /// assert!(push_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 PushCommand {
    fn default() -> Self {
        Self::new("localhost/test:latest")
    }
}

#[async_trait]
impl DockerCommand for PushCommand {
    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!["push".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_push_command_basic() {
        let push_cmd = PushCommand::new("myapp:latest");
        let args = push_cmd.build_command_args();

        assert_eq!(args, vec!["push", "myapp:latest"]);
        assert_eq!(push_cmd.get_image(), "myapp:latest");
        assert!(!push_cmd.is_all_tags());
        assert!(!push_cmd.is_quiet());
        assert!(!push_cmd.is_content_trust_disabled());
        assert_eq!(push_cmd.get_platform(), None);
    }

    #[test]
    fn test_push_command_with_all_tags() {
        let push_cmd = PushCommand::new("myregistry.com/myapp").all_tags();
        let args = push_cmd.build_command_args();

        assert!(args.contains(&"--all-tags".to_string()));
        assert!(args.contains(&"myregistry.com/myapp".to_string()));
        assert_eq!(args[0], "push");
        assert!(push_cmd.is_all_tags());
    }

    #[test]
    fn test_push_command_with_platform() {
        let push_cmd = PushCommand::new("myapp:latest").platform("linux/arm64");
        let args = push_cmd.build_command_args();

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

    #[test]
    fn test_push_command_with_quiet() {
        let push_cmd = PushCommand::new("myapp:v1.0").quiet();
        let args = push_cmd.build_command_args();

        assert!(args.contains(&"--quiet".to_string()));
        assert!(args.contains(&"myapp:v1.0".to_string()));
        assert_eq!(args[0], "push");
        assert!(push_cmd.is_quiet());
    }

    #[test]
    fn test_push_command_disable_content_trust() {
        let push_cmd = PushCommand::new("registry.com/myapp:stable").disable_content_trust();
        let args = push_cmd.build_command_args();

        assert!(args.contains(&"--disable-content-trust".to_string()));
        assert!(args.contains(&"registry.com/myapp:stable".to_string()));
        assert_eq!(args[0], "push");
        assert!(push_cmd.is_content_trust_disabled());
    }

    #[test]
    fn test_push_command_all_options() {
        let push_cmd = PushCommand::new("myregistry.io/myapp")
            .all_tags()
            .platform("linux/amd64")
            .quiet()
            .disable_content_trust();

        let args = push_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(&"myregistry.io/myapp".to_string()));
        assert_eq!(args[0], "push");

        // Verify helper methods
        assert!(push_cmd.is_all_tags());
        assert!(push_cmd.is_quiet());
        assert!(push_cmd.is_content_trust_disabled());
        assert_eq!(push_cmd.get_platform(), Some("linux/amd64"));
        assert_eq!(push_cmd.get_image(), "myregistry.io/myapp");
    }

    #[test]
    fn test_push_command_with_registry_and_namespace() {
        let push_cmd = PushCommand::new("registry.example.com:5000/namespace/myapp:v2.1");
        let args = push_cmd.build_command_args();

        assert_eq!(
            args,
            vec!["push", "registry.example.com:5000/namespace/myapp:v2.1"]
        );
        assert_eq!(
            push_cmd.get_image(),
            "registry.example.com:5000/namespace/myapp:v2.1"
        );
    }

    #[test]
    fn test_push_command_docker_hub_format() {
        let push_cmd = PushCommand::new("username/repository:tag");
        let args = push_cmd.build_command_args();

        assert_eq!(args, vec!["push", "username/repository:tag"]);
        assert_eq!(push_cmd.get_image(), "username/repository:tag");
    }

    #[test]
    fn test_push_command_order() {
        let push_cmd = PushCommand::new("myapp:latest")
            .quiet()
            .platform("linux/arm64")
            .all_tags();

        let args = push_cmd.build_command_args();

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

        // Image should be last
        assert_eq!(args.last(), Some(&"myapp:latest".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_push_command_default() {
        let push_cmd = PushCommand::default();
        assert_eq!(push_cmd.get_image(), "localhost/test:latest");
    }

    #[test]
    fn test_push_command_local_registry() {
        let push_cmd = PushCommand::new("localhost:5000/myapp:dev");
        let args = push_cmd.build_command_args();

        assert_eq!(args, vec!["push", "localhost:5000/myapp:dev"]);
        assert_eq!(push_cmd.get_image(), "localhost:5000/myapp:dev");
    }
}