add delete subcommand

This commit is contained in:
Dustin J. Mitchell 2020-11-29 13:45:58 -05:00
parent 29ab993397
commit af7363f3aa
2 changed files with 54 additions and 0 deletions

52
cli/src/cmd/delete.rs Normal file
View file

@ -0,0 +1,52 @@
use clap::{App, ArgMatches, SubCommand as ClapSubCommand};
use failure::Fallible;
use taskchampion::Status;
use crate::cmd::{shared, ArgMatchResult, CommandInvocation};
#[derive(Debug)]
struct Invocation {
task: String,
}
define_subcommand! {
fn decorate_app<'a>(&self, app: App<'a, 'a>) -> App<'a, 'a> {
app.subcommand(
ClapSubCommand::with_name("delete")
.about("mark the given task as deleted")
.arg(shared::task_arg()))
}
fn arg_match<'a>(&self, matches: &ArgMatches<'a>) -> ArgMatchResult {
match matches.subcommand() {
("delete", Some(matches)) => ArgMatchResult::Ok(Box::new(Invocation {
task: matches.value_of("task").unwrap().into(),
})),
_ => ArgMatchResult::None,
}
}
}
subcommand_invocation! {
fn run(&self, command: &CommandInvocation) -> Fallible<()> {
let mut replica = command.get_replica()?;
let task = shared::get_task(&mut replica, &self.task)?;
let mut task = task.into_mut(&mut replica);
task.stop()?;
task.set_status(Status::Deleted)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_command() {
with_subcommand_invocation!(vec!["task", "delete", "1"], |inv: &Invocation| {
assert_eq!(inv.task, "1".to_string());
});
}
}

View file

@ -7,6 +7,7 @@ mod shared;
mod add; mod add;
mod debug; mod debug;
mod delete;
mod done; mod done;
mod gc; mod gc;
mod info; mod info;
@ -22,6 +23,7 @@ pub(crate) fn subcommands() -> Vec<Box<dyn SubCommand>> {
vec![ vec![
add::cmd(), add::cmd(),
debug::cmd(), debug::cmd(),
delete::cmd(),
done::cmd(), done::cmd(),
gc::cmd(), gc::cmd(),
info::cmd(), info::cmd(),