docker_wrapper/command/builder/
stop.rs

1//! Docker buildx stop command implementation.
2
3use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
4use crate::error::Result;
5use async_trait::async_trait;
6
7/// Result of buildx stop command
8#[derive(Debug, Clone)]
9pub struct BuildxStopResult {
10    /// The name of the builder that was stopped
11    pub name: Option<String>,
12    /// Raw output from the command
13    pub output: String,
14    /// Whether the command succeeded
15    pub success: bool,
16}
17
18impl BuildxStopResult {
19    /// Parse the buildx stop output
20    fn parse(name: Option<&str>, output: &CommandOutput) -> Self {
21        Self {
22            name: name.map(ToString::to_string),
23            output: output.stdout.clone(),
24            success: output.success,
25        }
26    }
27}
28
29/// Docker buildx stop command builder
30///
31/// Stops a builder instance.
32///
33/// # Example
34///
35/// ```rust,no_run
36/// use docker_wrapper::{DockerCommand, BuildxStopCommand};
37///
38/// # #[tokio::main]
39/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
40/// let result = BuildxStopCommand::new()
41///     .name("mybuilder")
42///     .execute()
43///     .await?;
44///
45/// if result.success {
46///     println!("Builder stopped successfully");
47/// }
48/// # Ok(())
49/// # }
50/// ```
51#[derive(Debug, Clone, Default)]
52pub struct BuildxStopCommand {
53    /// The builder name to stop (optional, defaults to current)
54    name: Option<String>,
55    /// Command executor
56    pub executor: CommandExecutor,
57}
58
59impl BuildxStopCommand {
60    /// Create a new buildx stop command
61    #[must_use]
62    pub fn new() -> Self {
63        Self::default()
64    }
65
66    /// Set the builder name to stop
67    #[must_use]
68    pub fn name(mut self, name: impl Into<String>) -> Self {
69        self.name = Some(name.into());
70        self
71    }
72
73    /// Build the command arguments
74    fn build_args(&self) -> Vec<String> {
75        let mut args = vec!["buildx".to_string(), "stop".to_string()];
76
77        if let Some(ref name) = self.name {
78            args.push(name.clone());
79        }
80
81        args.extend(self.executor.raw_args.clone());
82
83        args
84    }
85}
86
87#[async_trait]
88impl DockerCommand for BuildxStopCommand {
89    type Output = BuildxStopResult;
90
91    fn get_executor(&self) -> &CommandExecutor {
92        &self.executor
93    }
94
95    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
96        &mut self.executor
97    }
98
99    fn build_command_args(&self) -> Vec<String> {
100        self.build_args()
101    }
102
103    async fn execute(&self) -> Result<Self::Output> {
104        let args = self.build_args();
105        let output = self.execute_command(args).await?;
106        Ok(BuildxStopResult::parse(self.name.as_deref(), &output))
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn test_buildx_stop_basic() {
116        let cmd = BuildxStopCommand::new();
117        let args = cmd.build_args();
118        assert_eq!(args, vec!["buildx", "stop"]);
119    }
120
121    #[test]
122    fn test_buildx_stop_with_name() {
123        let cmd = BuildxStopCommand::new().name("mybuilder");
124        let args = cmd.build_args();
125        assert!(args.contains(&"mybuilder".to_string()));
126    }
127}