use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct TagCommand {
pub executor: CommandExecutor,
pub name: Option<String>,
pub commit: Option<String>,
pub annotated: bool,
pub message: Option<String>,
pub signed: bool,
pub force: bool,
pub delete: bool,
pub list: bool,
pub pattern: Option<String>,
}
impl TagCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn name(&mut self, n: impl Into<String>) -> &mut Self {
self.name = Some(n.into());
self
}
pub fn commit(&mut self, c: impl Into<String>) -> &mut Self {
self.commit = Some(c.into());
self
}
pub fn annotated(&mut self) -> &mut Self {
self.annotated = true;
self
}
pub fn message(&mut self, m: impl Into<String>) -> &mut Self {
self.message = Some(m.into());
self.annotated = true;
self
}
pub fn signed(&mut self) -> &mut Self {
self.signed = true;
self
}
pub fn force(&mut self) -> &mut Self {
self.force = true;
self
}
pub fn delete(&mut self) -> &mut Self {
self.delete = true;
self
}
pub fn list(&mut self) -> &mut Self {
self.list = true;
self
}
pub fn pattern(&mut self, p: impl Into<String>) -> &mut Self {
self.pattern = Some(p.into());
self.list = true;
self
}
}
#[async_trait]
impl GitCommand for TagCommand {
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!["tag".to_string()];
if self.delete {
args.push("-d".into());
if let Some(n) = &self.name {
args.push(n.clone());
}
return args;
}
if self.list {
args.push("-l".into());
if let Some(p) = &self.pattern {
args.push(p.clone());
}
return args;
}
if self.annotated {
args.push("-a".into());
}
if self.signed {
args.push("-s".into());
}
if self.force {
args.push("-f".into());
}
if let Some(m) = &self.message {
args.push("-m".into());
args.push(m.clone());
}
if let Some(n) = &self.name {
args.push(n.clone());
}
if let Some(c) = &self.commit {
args.push(c.clone());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}