use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)] pub struct ComposeCreateCommand {
pub executor: CommandExecutor,
pub config: ComposeConfig,
pub build: bool,
pub no_build: bool,
pub force_recreate: bool,
pub no_recreate: bool,
pub pull: Option<PullPolicy>,
pub remove_orphans: bool,
pub services: Vec<String>,
}
#[derive(Debug, Clone, Copy)]
pub enum PullPolicy {
Always,
Never,
Missing,
Build,
}
impl std::fmt::Display for PullPolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Always => write!(f, "always"),
Self::Never => write!(f, "never"),
Self::Missing => write!(f, "missing"),
Self::Build => write!(f, "build"),
}
}
}
#[derive(Debug, Clone)]
pub struct ComposeCreateResult {
pub stdout: String,
pub stderr: String,
pub success: bool,
pub services: Vec<String>,
}
impl ComposeCreateCommand {
#[must_use]
pub fn new() -> Self {
Self {
executor: CommandExecutor::new(),
config: ComposeConfig::new(),
build: false,
no_build: false,
force_recreate: false,
no_recreate: false,
pull: None,
remove_orphans: false,
services: Vec::new(),
}
}
#[must_use]
pub fn build(mut self) -> Self {
self.build = true;
self
}
#[must_use]
pub fn no_build(mut self) -> Self {
self.no_build = true;
self
}
#[must_use]
pub fn force_recreate(mut self) -> Self {
self.force_recreate = true;
self
}
#[must_use]
pub fn no_recreate(mut self) -> Self {
self.no_recreate = true;
self
}
#[must_use]
pub fn pull(mut self, policy: PullPolicy) -> Self {
self.pull = Some(policy);
self
}
#[must_use]
pub fn remove_orphans(mut self) -> Self {
self.remove_orphans = true;
self
}
#[must_use]
pub fn service(mut self, service: impl Into<String>) -> Self {
self.services.push(service.into());
self
}
#[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
}
}
impl Default for ComposeCreateCommand {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl DockerCommand for ComposeCreateCommand {
type Output = ComposeCreateResult;
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(ComposeCreateResult {
stdout: output.stdout,
stderr: output.stderr,
success: output.success,
services: self.services.clone(),
})
}
}
impl ComposeCommand for ComposeCreateCommand {
fn get_config(&self) -> &ComposeConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ComposeConfig {
&mut self.config
}
fn subcommand(&self) -> &'static str {
"create"
}
fn build_subcommand_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.build {
args.push("--build".to_string());
}
if self.no_build {
args.push("--no-build".to_string());
}
if self.force_recreate {
args.push("--force-recreate".to_string());
}
if self.no_recreate {
args.push("--no-recreate".to_string());
}
if self.remove_orphans {
args.push("--remove-orphans".to_string());
}
if let Some(pull) = self.pull {
args.push("--pull".to_string());
args.push(pull.to_string());
}
args.extend(self.services.clone());
args
}
}
impl ComposeCreateResult {
#[must_use]
pub fn success(&self) -> bool {
self.success
}
#[must_use]
pub fn services(&self) -> &[String] {
&self.services
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_create_basic() {
let cmd = ComposeCreateCommand::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(&"create".to_string()));
}
#[test]
fn test_compose_create_with_build() {
let cmd = ComposeCreateCommand::new().build().force_recreate();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--build".to_string()));
assert!(args.contains(&"--force-recreate".to_string()));
}
#[test]
fn test_compose_create_with_pull() {
let cmd = ComposeCreateCommand::new()
.pull(PullPolicy::Always)
.no_recreate();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--pull".to_string()));
assert!(args.contains(&"always".to_string()));
assert!(args.contains(&"--no-recreate".to_string()));
}
#[test]
fn test_compose_create_with_services() {
let cmd = ComposeCreateCommand::new()
.service("web")
.service("db")
.remove_orphans();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"web".to_string()));
assert!(args.contains(&"db".to_string()));
assert!(args.contains(&"--remove-orphans".to_string()));
}
#[test]
fn test_pull_policy_display() {
assert_eq!(PullPolicy::Always.to_string(), "always");
assert_eq!(PullPolicy::Never.to_string(), "never");
assert_eq!(PullPolicy::Missing.to_string(), "missing");
assert_eq!(PullPolicy::Build.to_string(), "build");
}
#[test]
fn test_compose_config_integration() {
let cmd = ComposeCreateCommand::new()
.file("docker-compose.yml")
.project_name("my-project")
.build()
.service("web");
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(&"my-project".to_string()));
assert!(args.contains(&"--build".to_string()));
assert!(args.contains(&"web".to_string()));
}
}