Skip to content

Commit 053dacc

Browse files
committed
feat: organize compose commands
1 parent aad5f40 commit 053dacc

33 files changed

Lines changed: 157 additions & 749 deletions

src/command.rs

Lines changed: 5 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,15 @@ use crate::platform::PlatformInfo;
99
use async_trait::async_trait;
1010
use std::collections::HashMap;
1111
use std::ffi::OsStr;
12-
use std::path::PathBuf;
1312
use std::process::Stdio;
1413
use tokio::process::Command as TokioCommand;
1514

16-
// Re-export all command modules
15+
// re-exports all command modules
1716
pub mod attach;
1817
pub mod bake;
1918
pub mod build;
2019
pub mod builder;
2120
pub mod commit;
22-
pub mod compose_attach;
23-
pub mod compose_build;
24-
pub mod compose_create;
25-
pub mod compose_down;
26-
pub mod compose_exec;
27-
pub mod compose_kill;
28-
pub mod compose_logs;
29-
pub mod compose_ls;
30-
pub mod compose_pause;
31-
pub mod compose_ps;
32-
pub mod compose_restart;
33-
pub mod compose_rm;
34-
pub mod compose_run;
35-
pub mod compose_start;
36-
pub mod compose_stop;
37-
pub mod compose_unpause;
38-
pub mod compose_up;
3921
pub mod container_prune;
4022
pub mod context;
4123
pub mod cp;
@@ -81,13 +63,16 @@ pub mod version;
8163
pub mod volume;
8264
pub mod wait;
8365

66+
#[cfg(feature = "compose")]
67+
pub mod compose;
68+
8469
/// Unified trait for all Docker commands (both regular and compose).
8570
#[async_trait]
8671
pub trait DockerCommand {
8772
/// The output type this command produces.
8873
type Output;
8974

90-
/// Gets the command name (e.g., "compose", "pull")
75+
/// Gets the command name (e.g., `compose`, `build`, `run`).
9176
fn command_name() -> &'static str;
9277

9378
/// Gets the command executor for extensibility.
@@ -138,31 +123,6 @@ pub trait DockerCommand {
138123
}
139124
}
140125

141-
/// Base configuration for all compose commands.
142-
#[derive(Debug, Clone, Default)]
143-
pub struct ComposeConfig {
144-
/// Compose file paths (-f, --file).
145-
pub files: Vec<PathBuf>,
146-
/// Project name (-p, --project-name).
147-
pub project_name: Option<String>,
148-
/// Project directory (--project-directory).
149-
pub project_directory: Option<PathBuf>,
150-
/// Profiles to enable (--profile).
151-
pub profiles: Vec<String>,
152-
/// Environment file (--env-file).
153-
pub env_file: Option<PathBuf>,
154-
/// Run in compatibility mode.
155-
pub compatibility: bool,
156-
/// Execute in dry run mode.
157-
pub dry_run: bool,
158-
/// Progress output type.
159-
pub progress: Option<ProgressType>,
160-
/// ANSI control characters.
161-
pub ansi: Option<AnsiMode>,
162-
/// Max parallelism (-1 for unlimited).
163-
pub parallel: Option<i32>,
164-
}
165-
166126
/// Progress output type for compose commands.
167127
#[derive(Debug, Clone, Copy)]
168128
pub enum ProgressType {
@@ -211,204 +171,6 @@ impl std::fmt::Display for AnsiMode {
211171
}
212172
}
213173

214-
impl ComposeConfig {
215-
/// Creates a new compose configuration.
216-
#[must_use]
217-
pub fn new() -> Self {
218-
Self::default()
219-
}
220-
221-
/// Adds a compose file.
222-
#[must_use]
223-
pub fn file(mut self, path: impl Into<PathBuf>) -> Self {
224-
self.files.push(path.into());
225-
self
226-
}
227-
228-
/// Sets the project name.
229-
#[must_use]
230-
pub fn project_name(mut self, name: impl Into<String>) -> Self {
231-
self.project_name = Some(name.into());
232-
self
233-
}
234-
235-
/// Sets the project directory.
236-
#[must_use]
237-
pub fn project_directory(mut self, dir: impl Into<PathBuf>) -> Self {
238-
self.project_directory = Some(dir.into());
239-
self
240-
}
241-
242-
/// Adds a profile.
243-
#[must_use]
244-
pub fn profile(mut self, profile: impl Into<String>) -> Self {
245-
self.profiles.push(profile.into());
246-
self
247-
}
248-
249-
/// Sets environment file.
250-
#[must_use]
251-
pub fn env_file(mut self, path: impl Into<PathBuf>) -> Self {
252-
self.env_file = Some(path.into());
253-
self
254-
}
255-
256-
/// Enables compatibility mode.
257-
#[must_use]
258-
pub fn compatibility(mut self) -> Self {
259-
self.compatibility = true;
260-
self
261-
}
262-
263-
/// Enables dry run mode.
264-
#[must_use]
265-
pub fn dry_run(mut self) -> Self {
266-
self.dry_run = true;
267-
self
268-
}
269-
270-
/// Sets progress output type.
271-
#[must_use]
272-
pub fn progress(mut self, progress: ProgressType) -> Self {
273-
self.progress = Some(progress);
274-
self
275-
}
276-
277-
/// Sets ANSI mode.
278-
#[must_use]
279-
pub fn ansi(mut self, ansi: AnsiMode) -> Self {
280-
self.ansi = Some(ansi);
281-
self
282-
}
283-
284-
/// Sets max parallelism.
285-
#[must_use]
286-
pub fn parallel(mut self, parallel: i32) -> Self {
287-
self.parallel = Some(parallel);
288-
self
289-
}
290-
291-
/// Builds global compose arguments.
292-
#[must_use]
293-
pub fn build_global_args(&self) -> Vec<String> {
294-
let mut args = Vec::new();
295-
296-
// Adds compose files.
297-
for file in &self.files {
298-
args.push("--file".to_string());
299-
args.push(file.to_string_lossy().to_string());
300-
}
301-
302-
// Adds project name.
303-
if let Some(ref name) = self.project_name {
304-
args.push("--project-name".to_string());
305-
args.push(name.clone());
306-
}
307-
308-
// Adds project directory.
309-
if let Some(ref dir) = self.project_directory {
310-
args.push("--project-directory".to_string());
311-
args.push(dir.to_string_lossy().to_string());
312-
}
313-
314-
// Adds profiles.
315-
for profile in &self.profiles {
316-
args.push("--profile".to_string());
317-
args.push(profile.clone());
318-
}
319-
320-
// Adds environment file.
321-
if let Some(ref env_file) = self.env_file {
322-
args.push("--env-file".to_string());
323-
args.push(env_file.to_string_lossy().to_string());
324-
}
325-
326-
// Adds flags.
327-
if self.compatibility {
328-
args.push("--compatibility".to_string());
329-
}
330-
331-
if self.dry_run {
332-
args.push("--dry-run".to_string());
333-
}
334-
335-
// Adds progress type.
336-
if let Some(progress) = self.progress {
337-
args.push("--progress".to_string());
338-
args.push(progress.to_string());
339-
}
340-
341-
// Adds ANSI mode.
342-
if let Some(ansi) = self.ansi {
343-
args.push("--ansi".to_string());
344-
args.push(ansi.to_string());
345-
}
346-
347-
// Adds parallel limit.
348-
if let Some(parallel) = self.parallel {
349-
args.push("--parallel".to_string());
350-
args.push(parallel.to_string());
351-
}
352-
353-
args
354-
}
355-
}
356-
357-
/// Extended trait for Docker Compose commands.
358-
pub trait ComposeCommand: DockerCommand {
359-
/// Gets the compose configuration.
360-
fn config(&self) -> &ComposeConfig;
361-
362-
/// Gets the mutable compose configuration for builder pattern.
363-
fn config_mut(&mut self) -> &mut ComposeConfig;
364-
365-
/// Gets the compose subcommand name (e.g., `up`, `down`, `ps`).
366-
fn subcommand(&self) -> &'static str;
367-
368-
/// Builds command-specific arguments (without global compose args).
369-
fn build_subcommand_args(&self) -> Vec<String>;
370-
371-
/// Builds complete command arguments including "compose" and global args.
372-
/// This provides the implementation for `DockerCommandV2::build_command_args`.
373-
fn build_command_args(&self) -> Vec<String> {
374-
let mut args = vec!["compose".to_string()];
375-
376-
// Adds global compose arguments.
377-
args.extend(self.config().build_global_args());
378-
379-
// Adds the subcommand.
380-
args.push(self.subcommand().to_string());
381-
382-
// Adds command-specific arguments.
383-
args.extend(self.build_subcommand_args());
384-
385-
// Adds raw arguments from executor.
386-
args.extend(self.executor().raw_args.clone());
387-
388-
args
389-
}
390-
391-
/// Helper builder methods for common compose config options.
392-
#[must_use]
393-
fn file<P: Into<PathBuf>>(mut self, file: P) -> Self
394-
where
395-
Self: Sized,
396-
{
397-
self.config_mut().files.push(file.into());
398-
self
399-
}
400-
401-
/// Sets project name for compose command.
402-
#[must_use]
403-
fn project_name(mut self, name: impl Into<String>) -> Self
404-
where
405-
Self: Sized,
406-
{
407-
self.config_mut().project_name = Some(name.into());
408-
self
409-
}
410-
}
411-
412174
/// Common functionality for executing Docker commands.
413175
#[derive(Debug, Clone)]
414176
pub struct CommandExecutor {

src/command/compose.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// re-exports all compose command modules
2+
pub mod attach;
3+
pub mod build;
4+
pub mod config;
5+
pub mod convert;
6+
pub mod cp;
7+
pub mod create;
8+
pub mod down;
9+
pub mod events;
10+
pub mod exec;
11+
pub mod images;
12+
pub mod kill;
13+
pub mod logs;
14+
pub mod ls;
15+
pub mod pause;
16+
pub mod port;
17+
pub mod ps;
18+
pub mod push;
19+
pub mod restart;
20+
pub mod rm;
21+
pub mod run;
22+
pub mod scale;
23+
pub mod start;
24+
pub mod stop;
25+
pub mod top;
26+
pub mod unpause;
27+
pub mod up;
28+
pub mod version;
29+
pub mod wait;
30+
pub mod watch;

0 commit comments

Comments
 (0)