Merge pull request #71 from djmitche/issue7

Add start and stop commands
This commit is contained in:
Dustin J. Mitchell
2020-11-28 23:19:21 -05:00
committed by GitHub
9 changed files with 360 additions and 9 deletions

57
cli/src/cmd/debug.rs Normal file
View File

@@ -0,0 +1,57 @@
use crate::table;
use clap::{App, ArgMatches, SubCommand as ClapSubCommand};
use failure::Fallible;
use prettytable::{cell, row, Table};
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("debug")
.about("debug info for the given task")
.arg(shared::task_arg()))
}
fn arg_match<'a>(&self, matches: &ArgMatches<'a>) -> ArgMatchResult {
match matches.subcommand() {
("debug", 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 t = Table::new();
t.set_format(table::format());
t.set_titles(row![b->"key", b->"value"]);
for (k, v) in task.get_taskmap().iter() {
t.add_row(row![k, v]);
}
t.printstd();
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_command() {
with_subcommand_invocation!(vec!["task", "debug", "1"], |inv: &Invocation| {
assert_eq!(inv.task, "1".to_string());
});
}
}

View File

@@ -42,6 +42,7 @@ subcommand_invocation! {
}
t.add_row(row![b->"Description", task.get_description()]);
t.add_row(row![b->"Status", task.get_status()]);
t.add_row(row![b->"Active", task.is_active()]);
t.printstd();
Ok(())
}

View File

@@ -26,13 +26,17 @@ subcommand_invocation! {
let mut replica = command.get_replica()?;
let mut t = Table::new();
t.set_format(table::format());
t.set_titles(row![b->"id", b->"description"]);
t.set_titles(row![b->"id", b->"act", b->"description"]);
for (uuid, task) in replica.all_tasks().unwrap() {
let mut id = uuid.to_string();
if let Some(i) = replica.get_working_set_index(&uuid)? {
id = i.to_string();
}
t.add_row(row![id, task.get_description()]);
let active = match task.is_active() {
true => "*",
false => "",
};
t.add_row(row![id, active, task.get_description()]);
}
t.printstd();
Ok(())

View File

@@ -6,22 +6,28 @@ mod macros;
mod shared;
mod add;
mod debug;
mod gc;
mod info;
mod list;
mod modify;
mod pending;
mod start;
mod stop;
mod sync;
/// Get a list of all subcommands in this crate
pub(crate) fn subcommands() -> Vec<Box<dyn SubCommand>> {
vec![
add::cmd(),
debug::cmd(),
gc::cmd(),
info::cmd(),
list::cmd(),
modify::cmd(),
pending::cmd(),
start::cmd(),
stop::cmd(),
sync::cmd(),
]
}

View File

@@ -28,10 +28,14 @@ subcommand_invocation! {
let working_set = command.get_replica()?.working_set().unwrap();
let mut t = Table::new();
t.set_format(table::format());
t.set_titles(row![b->"id", b->"description"]);
t.set_titles(row![b->"id", b->"act", b->"description"]);
for (i, item) in working_set.iter().enumerate() {
if let Some(ref task) = item {
t.add_row(row![i, task.get_description()]);
let active = match task.is_active() {
true => "*",
false => "",
};
t.add_row(row![i, active, task.get_description()]);
}
}
t.printstd();

48
cli/src/cmd/start.rs Normal file
View File

@@ -0,0 +1,48 @@
use clap::{App, ArgMatches, SubCommand as ClapSubCommand};
use failure::Fallible;
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("start")
.about("start the given task")
.arg(shared::task_arg()))
}
fn arg_match<'a>(&self, matches: &ArgMatches<'a>) -> ArgMatchResult {
match matches.subcommand() {
("start", 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)?;
task.into_mut(&mut replica).start()?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_command() {
with_subcommand_invocation!(vec!["task", "start", "1"], |inv: &Invocation| {
assert_eq!(inv.task, "1".to_string());
});
}
}

48
cli/src/cmd/stop.rs Normal file
View File

@@ -0,0 +1,48 @@
use clap::{App, ArgMatches, SubCommand as ClapSubCommand};
use failure::Fallible;
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("stop")
.about("stop the given task")
.arg(shared::task_arg()))
}
fn arg_match<'a>(&self, matches: &ArgMatches<'a>) -> ArgMatchResult {
match matches.subcommand() {
("stop", 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)?;
task.into_mut(&mut replica).stop()?;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_command() {
with_subcommand_invocation!(vec!["task", "stop", "1"], |inv: &Invocation| {
assert_eq!(inv.task, "1".to_string());
});
}
}