use crate::command::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct ComposeScaleCommand {
pub executor: CommandExecutor,
pub config: ComposeConfig,
pub services: HashMap<String, u32>,
pub no_deps: bool,
}
#[derive(Debug, Clone)]
pub struct ComposeScaleResult {
pub stdout: String,
pub stderr: String,
pub success: bool,
pub scaled_services: HashMap<String, u32>,
}
impl ComposeScaleCommand {
#[must_use]
pub fn new() -> Self {
Self {
executor: CommandExecutor::new(),
config: ComposeConfig::new(),
services: HashMap::new(),
no_deps: false,
}
}
#[must_use]
pub fn service(mut self, service: impl Into<String>, replicas: u32) -> Self {
self.services.insert(service.into(), replicas);
self
}
#[must_use]
pub fn services(mut self, services: HashMap<String, u32>) -> Self {
self.services.extend(services);
self
}
#[must_use]
pub fn no_deps(mut self) -> Self {
self.no_deps = true;
self
}
}
impl Default for ComposeScaleCommand {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl DockerCommand for ComposeScaleCommand {
type Output = ComposeScaleResult;
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(ComposeScaleResult {
stdout: output.stdout,
stderr: output.stderr,
success: output.success,
scaled_services: self.services.clone(),
})
}
}
impl ComposeCommand for ComposeScaleCommand {
fn get_config(&self) -> &ComposeConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ComposeConfig {
&mut self.config
}
fn subcommand(&self) -> &'static str {
"scale"
}
fn build_subcommand_args(&self) -> Vec<String> {
let mut args = Vec::new();
if self.no_deps {
args.push("--no-deps".to_string());
}
for (service, replicas) in &self.services {
args.push(format!("{service}={replicas}"));
}
args
}
}
impl ComposeScaleResult {
#[must_use]
pub fn success(&self) -> bool {
self.success
}
#[must_use]
pub fn scaled_services(&self) -> &HashMap<String, u32> {
&self.scaled_services
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compose_scale_basic() {
let cmd = ComposeScaleCommand::new().service("web", 3);
let args = cmd.build_subcommand_args();
assert!(args.contains(&"web=3".to_string()));
let full_args = ComposeCommand::build_command_args(&cmd);
assert_eq!(full_args[0], "compose");
assert!(full_args.contains(&"scale".to_string()));
}
#[test]
fn test_compose_scale_multiple_services() {
let cmd = ComposeScaleCommand::new()
.service("web", 3)
.service("worker", 2);
let args = cmd.build_subcommand_args();
assert!(args.contains(&"web=3".to_string()));
assert!(args.contains(&"worker=2".to_string()));
}
#[test]
fn test_compose_scale_no_deps() {
let cmd = ComposeScaleCommand::new().service("api", 5).no_deps();
let args = cmd.build_subcommand_args();
assert!(args.contains(&"--no-deps".to_string()));
assert!(args.contains(&"api=5".to_string()));
}
}