use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct RmCommand {
pub executor: CommandExecutor,
pub paths: Vec<String>,
pub cached: bool,
pub recursive: bool,
pub force: bool,
pub dry_run: bool,
pub quiet: bool,
}
impl RmCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn path(&mut self, p: impl Into<String>) -> &mut Self {
self.paths.push(p.into());
self
}
pub fn cached(&mut self) -> &mut Self {
self.cached = true;
self
}
pub fn recursive(&mut self) -> &mut Self {
self.recursive = true;
self
}
pub fn force(&mut self) -> &mut Self {
self.force = true;
self
}
pub fn dry_run(&mut self) -> &mut Self {
self.dry_run = true;
self
}
pub fn quiet(&mut self) -> &mut Self {
self.quiet = true;
self
}
}
#[async_trait]
impl GitCommand for RmCommand {
type Output = CommandOutput;
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> {
let mut args = vec!["rm".to_string()];
if self.cached {
args.push("--cached".into());
}
if self.recursive {
args.push("-r".into());
}
if self.force {
args.push("--force".into());
}
if self.dry_run {
args.push("--dry-run".into());
}
if self.quiet {
args.push("--quiet".into());
}
if !self.paths.is_empty() {
args.push("--".into());
args.extend(self.paths.iter().cloned());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}