use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct BuildxStopResult {
pub name: Option<String>,
pub output: String,
pub success: bool,
}
impl BuildxStopResult {
fn parse(name: Option<&str>, output: &CommandOutput) -> Self {
Self {
name: name.map(ToString::to_string),
output: output.stdout.clone(),
success: output.success,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct BuildxStopCommand {
name: Option<String>,
pub executor: CommandExecutor,
}
impl BuildxStopCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
fn build_args(&self) -> Vec<String> {
let mut args = vec!["buildx".to_string(), "stop".to_string()];
if let Some(ref name) = self.name {
args.push(name.clone());
}
args.extend(self.executor.raw_args.clone());
args
}
}
#[async_trait]
impl DockerCommand for BuildxStopCommand {
type Output = BuildxStopResult;
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(BuildxStopResult::parse(self.name.as_deref(), &output))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_buildx_stop_basic() {
let cmd = BuildxStopCommand::new();
let args = cmd.build_args();
assert_eq!(args, vec!["buildx", "stop"]);
}
#[test]
fn test_buildx_stop_with_name() {
let cmd = BuildxStopCommand::new().name("mybuilder");
let args = cmd.build_args();
assert!(args.contains(&"mybuilder".to_string()));
}
}