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
//! Docker load command implementation.
//!
//! This module provides the `docker load` command for loading Docker images from tar archives.
use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use std::path::Path;
/// Docker load command builder
///
/// Load an image from a tar archive or STDIN.
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::LoadCommand;
/// use std::path::Path;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Load image from file
/// let result = LoadCommand::new()
/// .input(Path::new("alpine.tar"))
/// .run()
/// .await?;
///
/// println!("Loaded images: {:?}", result.loaded_images());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct LoadCommand {
/// Input file path
input: Option<String>,
/// Suppress progress output during load
quiet: bool,
/// Command executor
pub executor: CommandExecutor,
}
impl LoadCommand {
/// Create a new load command
///
/// # Example
///
/// ```
/// use docker_wrapper::LoadCommand;
///
/// let cmd = LoadCommand::new();
/// ```
#[must_use]
pub fn new() -> Self {
Self {
input: None,
quiet: false,
executor: CommandExecutor::new(),
}
}
/// Set input file path
///
/// # Example
///
/// ```
/// use docker_wrapper::LoadCommand;
/// use std::path::Path;
///
/// let cmd = LoadCommand::new()
/// .input(Path::new("images.tar"));
/// ```
#[must_use]
pub fn input(mut self, path: &Path) -> Self {
self.input = Some(path.to_string_lossy().into_owned());
self
}
/// Suppress progress output during load
///
/// # Example
///
/// ```
/// use docker_wrapper::LoadCommand;
///
/// let cmd = LoadCommand::new().quiet();
/// ```
#[must_use]
pub fn quiet(mut self) -> Self {
self.quiet = true;
self
}
/// Execute the load command
///
/// # Errors
/// Returns an error if:
/// - The Docker daemon is not running
/// - The input file doesn't exist or is not readable
/// - The tar archive is corrupted or invalid
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::LoadCommand;
/// use std::path::Path;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let result = LoadCommand::new()
/// .input(Path::new("alpine.tar"))
/// .run()
/// .await?;
///
/// if result.success() {
/// println!("Images loaded successfully");
/// for image in result.loaded_images() {
/// println!(" - {}", image);
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn run(&self) -> Result<LoadResult> {
let output = self.execute().await?;
// Parse loaded images from output
let loaded_images = Self::parse_loaded_images(&output.stdout);
Ok(LoadResult {
output,
loaded_images,
})
}
/// Parse loaded image names from the command output
fn parse_loaded_images(stdout: &str) -> Vec<String> {
let mut images = Vec::new();
for line in stdout.lines() {
if line.starts_with("Loaded image:") {
if let Some(image) = line.strip_prefix("Loaded image:") {
images.push(image.trim().to_string());
}
} else if line.starts_with("Loaded image ID:") {
if let Some(id) = line.strip_prefix("Loaded image ID:") {
images.push(id.trim().to_string());
}
}
}
images
}
}
impl Default for LoadCommand {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl DockerCommand for LoadCommand {
type Output = CommandOutput;
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["load".to_string()];
if let Some(ref input_file) = self.input {
args.push("--input".to_string());
args.push(input_file.clone());
}
if self.quiet {
args.push("--quiet".to_string());
}
args.extend(self.executor.raw_args.clone());
args
}
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
let command_name = args[0].clone();
let command_args = args[1..].to_vec();
self.executor
.execute_command(&command_name, command_args)
.await
}
}
/// Result from the load command
#[derive(Debug, Clone)]
pub struct LoadResult {
/// Raw command output
pub output: CommandOutput,
/// List of loaded image names/IDs
pub loaded_images: Vec<String>,
}
impl LoadResult {
/// Check if the load was successful
#[must_use]
pub fn success(&self) -> bool {
self.output.success
}
/// Get the list of loaded images
#[must_use]
pub fn loaded_images(&self) -> &[String] {
&self.loaded_images
}
/// Get the count of loaded images
#[must_use]
pub fn image_count(&self) -> usize {
self.loaded_images.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_load_basic() {
let cmd = LoadCommand::new();
let args = cmd.build_command_args();
assert_eq!(args, vec!["load"]);
}
#[test]
fn test_load_with_input() {
let cmd = LoadCommand::new().input(Path::new("images.tar"));
let args = cmd.build_command_args();
assert_eq!(args, vec!["load", "--input", "images.tar"]);
}
#[test]
fn test_load_with_quiet() {
let cmd = LoadCommand::new().quiet();
let args = cmd.build_command_args();
assert_eq!(args, vec!["load", "--quiet"]);
}
#[test]
fn test_load_with_all_options() {
let cmd = LoadCommand::new()
.input(Path::new("/tmp/alpine.tar"))
.quiet();
let args = cmd.build_command_args();
assert_eq!(args, vec!["load", "--input", "/tmp/alpine.tar", "--quiet"]);
}
#[test]
fn test_parse_loaded_images() {
let output =
"Loaded image: alpine:latest\nLoaded image: nginx:1.21\nLoaded image ID: sha256:abc123";
let images = LoadCommand::parse_loaded_images(output);
assert_eq!(images, vec!["alpine:latest", "nginx:1.21", "sha256:abc123"]);
}
#[test]
fn test_parse_loaded_images_empty() {
let output = "";
let images = LoadCommand::parse_loaded_images(output);
assert!(images.is_empty());
}
}