use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct AddCommand {
pub executor: CommandExecutor,
pub paths: Vec<String>,
pub all: bool,
pub update: bool,
pub force: bool,
pub dry_run: bool,
pub verbose: bool,
pub intent_to_add: bool,
pub patch: bool,
pub ignore_errors: bool,
}
impl AddCommand {
#[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 paths<I, S>(&mut self, ps: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.paths.extend(ps.into_iter().map(Into::into));
self
}
pub fn all(&mut self) -> &mut Self {
self.all = true;
self
}
pub fn update(&mut self) -> &mut Self {
self.update = 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 verbose(&mut self) -> &mut Self {
self.verbose = true;
self
}
pub fn intent_to_add(&mut self) -> &mut Self {
self.intent_to_add = true;
self
}
pub fn patch(&mut self) -> &mut Self {
self.patch = true;
self
}
pub fn ignore_errors(&mut self) -> &mut Self {
self.ignore_errors = true;
self
}
}
#[async_trait]
impl GitCommand for AddCommand {
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!["add".to_string()];
if self.all {
args.push("--all".into());
}
if self.update {
args.push("--update".into());
}
if self.force {
args.push("--force".into());
}
if self.dry_run {
args.push("--dry-run".into());
}
if self.verbose {
args.push("--verbose".into());
}
if self.intent_to_add {
args.push("--intent-to-add".into());
}
if self.patch {
args.push("--patch".into());
}
if self.ignore_errors {
args.push("--ignore-errors".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
}
}