forked from joshrotenberg/docker-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.rs
More file actions
311 lines (264 loc) · 8.41 KB
/
pull.rs
File metadata and controls
311 lines (264 loc) · 8.41 KB
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
//! Docker Compose pull command implementation using unified trait pattern.
use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
/// Pull policy for compose pull command
#[derive(Debug, Clone, Copy)]
pub enum ComposePullPolicy {
/// Always pull images
Always,
/// Pull missing images only
Missing,
}
impl std::fmt::Display for ComposePullPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Always => write!(f, "always"),
Self::Missing => write!(f, "missing"),
}
}
}
/// Docker Compose pull command builder
#[allow(clippy::struct_excessive_bools)] // Multiple boolean flags are appropriate for pull command
#[derive(Debug, Clone)]
pub struct ComposePullCommand {
/// Base command executor
pub executor: CommandExecutor,
/// Base compose configuration
pub config: ComposeConfig,
/// Services to pull images for (empty for all)
pub services: Vec<String>,
/// Ignore images that can be built
pub ignore_buildable: bool,
/// Pull what it can and ignore images with pull failures
pub ignore_pull_failures: bool,
/// Also pull services declared as dependencies
pub include_deps: bool,
/// Pull policy
pub policy: Option<ComposePullPolicy>,
/// Pull without printing progress information
pub quiet: bool,
}
/// Result from compose pull command
#[derive(Debug, Clone)]
pub struct ComposePullResult {
/// Raw stdout output
pub stdout: String,
/// Raw stderr output
pub stderr: String,
/// Success status
pub success: bool,
/// Services that were pulled
pub services: Vec<String>,
}
impl ComposePullCommand {
/// Create a new compose pull command
#[must_use]
pub fn new() -> Self {
Self {
executor: CommandExecutor::new(),
config: ComposeConfig::new(),
services: Vec::new(),
ignore_buildable: false,
ignore_pull_failures: false,
include_deps: false,
policy: None,
quiet: false,
}
}
/// Add a service to pull
#[must_use]
pub fn service(mut self, service: impl Into<String>) -> Self {
self.services.push(service.into());
self
}
/// Add multiple services to pull
#[must_use]
pub fn services<I, S>(mut self, services: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.services.extend(services.into_iter().map(Into::into));
self
}
/// Ignore images that can be built
#[must_use]
pub fn ignore_buildable(mut self) -> Self {
self.ignore_buildable = true;
self
}
/// Pull what it can and ignore images with pull failures
#[must_use]
pub fn ignore_pull_failures(mut self) -> Self {
self.ignore_pull_failures = true;
self
}
/// Also pull services declared as dependencies
#[must_use]
pub fn include_deps(mut self) -> Self {
self.include_deps = true;
self
}
/// Set pull policy
#[must_use]
pub fn policy(mut self, policy: ComposePullPolicy) -> Self {
self.policy = Some(policy);
self
}
/// Pull without printing progress information
#[must_use]
pub fn quiet(mut self) -> Self {
self.quiet = true;
self
}
}
impl Default for ComposePullCommand {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl DockerCommand for ComposePullCommand {
type Output = ComposePullResult;
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> {
<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(ComposePullResult {
stdout: output.stdout,
stderr: output.stderr,
success: output.success,
services: self.services.clone(),
})
}
}
impl ComposeCommand for ComposePullCommand {
fn get_config(&self) -> &ComposeConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ComposeConfig {
&mut self.config
}
fn subcommand(&self) -> &'static str {
"pull"
}
fn build_subcommand_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.ignore_buildable {
args.push("--ignore-buildable".to_string());
}
if self.ignore_pull_failures {
args.push("--ignore-pull-failures".to_string());
}
if self.include_deps {
args.push("--include-deps".to_string());
}
if let Some(ref policy) = self.policy {
args.push("--policy".to_string());
args.push(policy.to_string());
}
if self.quiet {
args.push("--quiet".to_string());
}
args.extend(self.services.clone());
args
}
}
impl ComposePullResult {
/// Check if the command was successful
#[must_use]
pub fn success(&self) -> bool {
self.success
}
/// Get the services that were pulled
#[must_use]
pub fn services(&self) -> &[String] {
&self.services
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_pull_basic() {
let cmd = ComposePullCommand::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(&"pull".to_string()));
}
#[test]
fn test_compose_pull_with_options() {
let cmd = ComposePullCommand::new()
.ignore_buildable()
.ignore_pull_failures()
.include_deps()
.quiet()
.service("web");
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--ignore-buildable".to_string()));
assert!(args.contains(&"--ignore-pull-failures".to_string()));
assert!(args.contains(&"--include-deps".to_string()));
assert!(args.contains(&"--quiet".to_string()));
assert!(args.contains(&"web".to_string()));
}
#[test]
fn test_compose_pull_with_policy() {
let cmd = ComposePullCommand::new()
.policy(ComposePullPolicy::Always)
.service("db");
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--policy".to_string()));
assert!(args.contains(&"always".to_string()));
assert!(args.contains(&"db".to_string()));
}
#[test]
fn test_compose_pull_with_missing_policy() {
let cmd = ComposePullCommand::new().policy(ComposePullPolicy::Missing);
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--policy".to_string()));
assert!(args.contains(&"missing".to_string()));
}
#[test]
fn test_compose_pull_multiple_services() {
let cmd = ComposePullCommand::new()
.service("web")
.service("db")
.service("redis");
let args = cmd.build_subcommand_args();
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"db".to_string()));
assert!(args.contains(&"redis".to_string()));
}
#[test]
fn test_compose_pull_services_batch() {
let cmd = ComposePullCommand::new().services(vec!["web", "db"]);
let args = cmd.build_subcommand_args();
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"db".to_string()));
}
#[test]
fn test_compose_pull_config_integration() {
let cmd = ComposePullCommand::new()
.file("docker-compose.yml")
.project_name("myapp")
.service("api");
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(&"pull".to_string()));
assert!(args.contains(&"api".to_string()));
}
}