docker_wrapper/command/
compose_kill.rs1use super::{CommandExecutor, ComposeCommand, ComposeConfig, DockerCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7#[derive(Debug, Clone)]
9pub struct ComposeKillCommand {
10 pub executor: CommandExecutor,
12 pub config: ComposeConfig,
14 pub services: Vec<String>,
16 pub signal: Option<String>,
18}
19
20#[derive(Debug, Clone)]
22pub struct ComposeKillResult {
23 pub stdout: String,
25 pub stderr: String,
27 pub success: bool,
29 pub services: Vec<String>,
31}
32
33impl ComposeKillCommand {
34 #[must_use]
36 pub fn new() -> Self {
37 Self {
38 executor: CommandExecutor::new(),
39 config: ComposeConfig::new(),
40 services: Vec::new(),
41 signal: None,
42 }
43 }
44
45 #[must_use]
47 pub fn service(mut self, service: impl Into<String>) -> Self {
48 self.services.push(service.into());
49 self
50 }
51
52 #[must_use]
54 pub fn services<I, S>(mut self, services: I) -> Self
55 where
56 I: IntoIterator<Item = S>,
57 S: Into<String>,
58 {
59 self.services.extend(services.into_iter().map(Into::into));
60 self
61 }
62
63 #[must_use]
65 pub fn signal(mut self, signal: impl Into<String>) -> Self {
66 self.signal = Some(signal.into());
67 self
68 }
69}
70
71impl Default for ComposeKillCommand {
72 fn default() -> Self {
73 Self::new()
74 }
75}
76
77#[async_trait]
78impl DockerCommand for ComposeKillCommand {
79 type Output = ComposeKillResult;
80
81 fn get_executor(&self) -> &CommandExecutor {
82 &self.executor
83 }
84
85 fn get_executor_mut(&mut self) -> &mut CommandExecutor {
86 &mut self.executor
87 }
88
89 fn build_command_args(&self) -> Vec<String> {
90 <Self as ComposeCommand>::build_command_args(self)
92 }
93
94 async fn execute(&self) -> Result<Self::Output> {
95 let args = <Self as ComposeCommand>::build_command_args(self);
96 let output = self.execute_command(args).await?;
97
98 Ok(ComposeKillResult {
99 stdout: output.stdout,
100 stderr: output.stderr,
101 success: output.success,
102 services: self.services.clone(),
103 })
104 }
105}
106
107impl ComposeCommand for ComposeKillCommand {
108 fn get_config(&self) -> &ComposeConfig {
109 &self.config
110 }
111
112 fn get_config_mut(&mut self) -> &mut ComposeConfig {
113 &mut self.config
114 }
115
116 fn subcommand(&self) -> &'static str {
117 "kill"
118 }
119
120 fn build_subcommand_args(&self) -> Vec<String> {
121 let mut args = Vec::new();
122
123 if let Some(ref signal) = self.signal {
124 args.push("--signal".to_string());
125 args.push(signal.clone());
126 }
127
128 args.extend(self.services.clone());
130
131 args
132 }
133}
134
135impl ComposeKillResult {
136 #[must_use]
138 pub fn success(&self) -> bool {
139 self.success
140 }
141
142 #[must_use]
144 pub fn services(&self) -> &[String] {
145 &self.services
146 }
147}
148
149#[cfg(test)]
150mod tests {
151 use super::*;
152
153 #[test]
154 fn test_compose_kill_basic() {
155 let cmd = ComposeKillCommand::new();
156 let args = cmd.build_subcommand_args();
157 assert!(args.is_empty());
158
159 let full_args = ComposeCommand::build_command_args(&cmd);
160 assert_eq!(full_args[0], "compose");
161 assert!(full_args.contains(&"kill".to_string()));
162 }
163
164 #[test]
165 fn test_compose_kill_with_services() {
166 let cmd = ComposeKillCommand::new().service("web").service("worker");
167 let args = cmd.build_subcommand_args();
168 assert_eq!(args, vec!["web", "worker"]);
169 }
170
171 #[test]
172 fn test_compose_kill_with_signal() {
173 let cmd = ComposeKillCommand::new().signal("SIGTERM").service("app");
174
175 let args = cmd.build_subcommand_args();
176 assert_eq!(args, vec!["--signal", "SIGTERM", "app"]);
177 }
178
179 #[test]
180 fn test_compose_kill_with_numeric_signal() {
181 let cmd = ComposeKillCommand::new().signal("15").service("database");
182
183 let args = cmd.build_subcommand_args();
184 assert_eq!(args, vec!["--signal", "15", "database"]);
185 }
186
187 #[test]
188 fn test_compose_kill_multiple_services() {
189 let cmd = ComposeKillCommand::new()
190 .services(vec!["frontend", "backend"])
191 .signal("SIGINT");
192
193 let args = cmd.build_subcommand_args();
194 assert_eq!(args, vec!["--signal", "SIGINT", "frontend", "backend"]);
195 }
196
197 #[test]
198 fn test_compose_config_integration() {
199 let cmd = ComposeKillCommand::new()
200 .file("docker-compose.yml")
201 .project_name("myapp")
202 .signal("SIGTERM")
203 .service("web");
204
205 let args = ComposeCommand::build_command_args(&cmd);
206 assert!(args.contains(&"--file".to_string()));
207 assert!(args.contains(&"docker-compose.yml".to_string()));
208 assert!(args.contains(&"--project-name".to_string()));
209 assert!(args.contains(&"myapp".to_string()));
210 assert!(args.contains(&"--signal".to_string()));
211 assert!(args.contains(&"SIGTERM".to_string()));
212 assert!(args.contains(&"web".to_string()));
213 }
214}