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
//! Docker export command implementation.
//!
//! This module provides the `docker export` command for exporting containers to tarballs.
use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
/// Docker export command builder
///
/// Export a container's filesystem as a tar archive.
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::ExportCommand;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Export container to file
/// let result = ExportCommand::new("my-container")
/// .output("container.tar")
/// .run()
/// .await?;
///
/// if result.success() {
/// println!("Container exported to container.tar");
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct ExportCommand {
/// Container name or ID to export
container: String,
/// Output file path
output: Option<String>,
/// Command executor
pub executor: CommandExecutor,
}
impl ExportCommand {
/// Create a new export command
///
/// # Example
///
/// ```
/// use docker_wrapper::ExportCommand;
///
/// let cmd = ExportCommand::new("my-container");
/// ```
#[must_use]
pub fn new(container: impl Into<String>) -> Self {
Self {
container: container.into(),
output: None,
executor: CommandExecutor::new(),
}
}
/// Set output file for the export
///
/// # Example
///
/// ```
/// use docker_wrapper::ExportCommand;
///
/// let cmd = ExportCommand::new("my-container")
/// .output("backup.tar");
/// ```
#[must_use]
pub fn output(mut self, output: impl Into<String>) -> Self {
self.output = Some(output.into());
self
}
/// Execute the export command
///
/// # Errors
/// Returns an error if:
/// - The Docker daemon is not running
/// - The container doesn't exist
/// - File I/O errors occur during export
/// - Insufficient disk space
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::ExportCommand;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let result = ExportCommand::new("web-server")
/// .output("web-backup.tar")
/// .run()
/// .await?;
///
/// if result.success() {
/// println!("Container '{}' exported to '{}'",
/// result.container(), result.output_file().unwrap_or("stdout"));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn run(&self) -> Result<ExportResult> {
let output = self.execute().await?;
Ok(ExportResult {
output,
container: self.container.clone(),
output_file: self.output.clone(),
})
}
}
#[async_trait]
impl DockerCommand for ExportCommand {
type Output = CommandOutput;
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["export".to_string()];
if let Some(ref output) = self.output {
args.push("--output".to_string());
args.push(output.clone());
}
args.push(self.container.clone());
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 export command
#[derive(Debug, Clone)]
pub struct ExportResult {
/// Raw command output
pub output: CommandOutput,
/// Container that was exported
pub container: String,
/// Output file path (if specified)
pub output_file: Option<String>,
}
impl ExportResult {
/// Check if the export was successful
#[must_use]
pub fn success(&self) -> bool {
self.output.success
}
/// Get the container name
#[must_use]
pub fn container(&self) -> &str {
&self.container
}
/// Get the output file path
#[must_use]
pub fn output_file(&self) -> Option<&str> {
self.output_file.as_deref()
}
/// Get the raw command output
#[must_use]
pub fn output(&self) -> &CommandOutput {
&self.output
}
/// Check if export was written to a file
#[must_use]
pub fn exported_to_file(&self) -> bool {
self.output_file.is_some()
}
/// Check if export was written to stdout
#[must_use]
pub fn exported_to_stdout(&self) -> bool {
self.output_file.is_none()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_export_basic() {
let cmd = ExportCommand::new("test-container");
let args = cmd.build_command_args();
assert_eq!(args, vec!["export", "test-container"]);
}
#[test]
fn test_export_with_output() {
let cmd = ExportCommand::new("test-container").output("backup.tar");
let args = cmd.build_command_args();
assert_eq!(
args,
vec!["export", "--output", "backup.tar", "test-container"]
);
}
#[test]
fn test_export_with_path() {
let cmd = ExportCommand::new("web-server").output("/tmp/exports/web.tar");
let args = cmd.build_command_args();
assert_eq!(
args,
vec!["export", "--output", "/tmp/exports/web.tar", "web-server"]
);
}
#[test]
fn test_export_result() {
let result = ExportResult {
output: CommandOutput {
stdout: String::new(),
stderr: String::new(),
exit_code: 0,
success: true,
},
container: "my-container".to_string(),
output_file: Some("backup.tar".to_string()),
};
assert!(result.success());
assert_eq!(result.container(), "my-container");
assert_eq!(result.output_file(), Some("backup.tar"));
assert!(result.exported_to_file());
assert!(!result.exported_to_stdout());
}
#[test]
fn test_export_result_stdout() {
let result = ExportResult {
output: CommandOutput {
stdout: "tar data...".to_string(),
stderr: String::new(),
exit_code: 0,
success: true,
},
container: "my-container".to_string(),
output_file: None,
};
assert!(result.success());
assert_eq!(result.container(), "my-container");
assert_eq!(result.output_file(), None);
assert!(!result.exported_to_file());
assert!(result.exported_to_stdout());
}
}