use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct NetworkRmCommand {
networks: Vec<String>,
force: bool,
pub executor: CommandExecutor,
}
impl NetworkRmCommand {
#[must_use]
pub fn new(network: impl Into<String>) -> Self {
Self {
networks: vec![network.into()],
force: false,
executor: CommandExecutor::new(),
}
}
#[must_use]
pub fn new_multiple(networks: Vec<String>) -> Self {
Self {
networks,
force: false,
executor: CommandExecutor::new(),
}
}
#[must_use]
pub fn add_network(mut self, network: impl Into<String>) -> Self {
self.networks.push(network.into());
self
}
#[must_use]
pub fn force(mut self) -> Self {
self.force = true;
self
}
pub async fn run(&self) -> Result<NetworkRmResult> {
self.execute().await.map(NetworkRmResult::from)
}
}
#[async_trait]
impl DockerCommand for NetworkRmCommand {
type Output = CommandOutput;
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["network".to_string(), "rm".to_string()];
if self.force {
args.push("--force".to_string());
}
for network in &self.networks {
args.push(network.clone());
}
args.extend(self.executor.raw_args.clone());
args
}
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
let command_name = args[0].clone();
let command_args = args[1..].to_vec();
self.executor
.execute_command(&command_name, command_args)
.await
}
}
#[derive(Debug, Clone)]
pub struct NetworkRmResult {
pub removed_networks: Vec<String>,
pub raw_output: CommandOutput,
}
impl From<CommandOutput> for NetworkRmResult {
fn from(output: CommandOutput) -> Self {
let removed_networks = output
.stdout
.lines()
.filter(|line| !line.is_empty())
.map(String::from)
.collect();
Self {
removed_networks,
raw_output: output,
}
}
}
impl NetworkRmResult {
#[must_use]
pub fn is_success(&self) -> bool {
self.raw_output.success
}
#[must_use]
pub fn count(&self) -> usize {
self.removed_networks.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_network_rm_single() {
let cmd = NetworkRmCommand::new("my-network");
let args = cmd.build_command_args();
assert_eq!(args, vec!["network", "rm", "my-network"]);
}
#[test]
fn test_network_rm_multiple() {
let cmd =
NetworkRmCommand::new_multiple(vec!["network1".to_string(), "network2".to_string()]);
let args = cmd.build_command_args();
assert_eq!(args, vec!["network", "rm", "network1", "network2"]);
}
#[test]
fn test_network_rm_force() {
let cmd = NetworkRmCommand::new("my-network").force();
let args = cmd.build_command_args();
assert_eq!(args, vec!["network", "rm", "--force", "my-network"]);
}
}