use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct SwarmLeaveResult {
pub success: bool,
pub output: String,
}
impl SwarmLeaveResult {
fn parse(output: &CommandOutput) -> Self {
Self {
success: output.success,
output: output.stdout.clone(),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct SwarmLeaveCommand {
force: bool,
pub executor: CommandExecutor,
}
impl SwarmLeaveCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn force(mut self) -> Self {
self.force = true;
self
}
fn build_args(&self) -> Vec<String> {
let mut args = vec!["swarm".to_string(), "leave".to_string()];
if self.force {
args.push("--force".to_string());
}
args
}
}
#[async_trait]
impl DockerCommand for SwarmLeaveCommand {
type Output = SwarmLeaveResult;
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.build_args()
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_args();
let output = self.execute_command(args).await?;
Ok(SwarmLeaveResult::parse(&output))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_swarm_leave_basic() {
let cmd = SwarmLeaveCommand::new();
let args = cmd.build_args();
assert_eq!(args, vec!["swarm", "leave"]);
}
#[test]
fn test_swarm_leave_force() {
let cmd = SwarmLeaveCommand::new().force();
let args = cmd.build_args();
assert!(args.contains(&"--force".to_string()));
}
}