imag/imag-todo/src/main.rs

160 lines
5.1 KiB
Rust
Raw Normal View History

extern crate clap;
extern crate glob;
#[macro_use] extern crate log;
2016-06-28 18:13:11 +00:00
extern crate serde_json;
extern crate semver;
extern crate toml;
#[macro_use] extern crate version;
2016-06-28 18:13:11 +00:00
extern crate task_hookrs;
extern crate libimagrt;
extern crate libimagstore;
extern crate libimagerror;
2016-06-28 18:13:11 +00:00
extern crate libimagtodo;
use std::process::exit;
use std::process::{Command, Stdio};
2016-06-28 18:13:11 +00:00
use std::io::stdin;
2016-07-21 13:54:38 +00:00
use toml::Value;
2016-07-15 19:37:51 +00:00
use task_hookrs::import::import_tasks;
use task_hookrs::status::TaskStatus;
use libimagrt::runtime::Runtime;
use libimagrt::setup::generate_runtime_setup;
2016-07-07 14:20:00 +00:00
use libimagtodo::task::Task;
use libimagerror::trace::trace_error;
mod ui;
use ui::build_ui;
fn main() {
let rt = generate_runtime_setup("imag-todo",
&version!()[..],
"Interface with taskwarrior",
build_ui);
2016-05-10 14:28:35 +00:00
2016-07-06 17:50:55 +00:00
match rt.cli().subcommand_name() {
2016-07-06 17:38:00 +00:00
Some("tw-hook") => tw_hook(&rt),
Some("list") => list(&rt),
2016-07-15 19:07:03 +00:00
_ => unreachable!(),
2016-07-06 17:38:00 +00:00
} // end match scmd
} // end main
fn tw_hook(rt: &Runtime) {
let subcmd = rt.cli().subcommand_matches("tw-hook").unwrap();
if subcmd.is_present("add") {
let stdin = stdin();
2016-08-06 08:07:26 +00:00
let stdin = stdin.lock(); // implements BufRead which is required for `Task::import()`
2016-07-06 17:50:55 +00:00
2016-07-10 14:35:24 +00:00
match Task::import(rt.store(), stdin) {
Ok((_, line, uuid)) => info!("{}\nTask {} stored in imag", line, uuid),
2016-07-10 14:35:24 +00:00
Err(e) => {
trace_error(&e);
exit(1);
2016-07-06 17:50:55 +00:00
}
2016-07-06 17:38:00 +00:00
}
2016-07-06 17:50:55 +00:00
} else if subcmd.is_present("delete") {
2016-07-06 17:38:00 +00:00
// The used hook is "on-modify". This hook gives two json-objects
// per usage und wants one (the second one) back.
2016-07-06 17:50:55 +00:00
let stdin = stdin();
let stdin = stdin.lock();
match import_tasks(stdin) {
Ok(ttasks) => for (counter, ttask) in ttasks.iter().enumerate() {
2016-07-06 17:38:00 +00:00
if counter % 2 == 1 {
// Only every second task is needed, the first one is the
// task before the change, and the second one after
// the change. The (maybe modified) second one is
// expected by taskwarrior.
2016-07-06 17:50:55 +00:00
match serde_json::ser::to_string(&ttask) {
Ok(val) => println!("{}", val),
2016-06-30 16:25:57 +00:00
Err(e) => {
2016-07-06 17:50:55 +00:00
trace_error(&e);
exit(1);
2016-06-30 16:25:57 +00:00
}
2016-07-06 17:50:55 +00:00
}
2016-08-06 08:14:01 +00:00
// Taskwarrior does not have the concept of deleted tasks, but only modified
// ones.
//
// Here we check if the status of a task is deleted and if yes, we delete it
// from the store.
if *ttask.status() == TaskStatus::Deleted {
match Task::delete_by_uuid(rt.store(), *ttask.uuid()) {
Ok(_) => println!("Deleted task {}", *ttask.uuid()),
Err(e) => {
trace_error(&e);
exit(1);
2016-06-28 21:05:05 +00:00
}
}
}
2016-07-06 17:38:00 +00:00
} // end if c % 2
},
Err(e) => {
trace_error(&e);
exit(1);
},
2016-07-06 17:38:00 +00:00
}
2016-07-06 17:50:55 +00:00
} else {
2016-07-06 17:38:00 +00:00
// Should not be possible, as one argument is required via
// ArgGroup
unreachable!();
}
}
fn list(rt: &Runtime) {
2016-07-21 13:54:38 +00:00
let subcmd = rt.cli().subcommand_matches("list").unwrap();
let verbose = subcmd.is_present("verbose");
let res = Task::all(rt.store())
.map(|iter| {
let uuids : Vec<_> = iter.filter_map(|t| match t {
Ok(v) => match v.get_header().read("todo.uuid") {
Ok(Some(Value::String(ref u))) => Some(u.clone()),
Ok(Some(_)) => {
warn!("Header type error");
None
},
Ok(None) => None,
Err(e) => {
2016-07-06 17:50:55 +00:00
trace_error(&e);
2016-07-21 13:54:38 +00:00
None
2016-06-28 21:05:05 +00:00
}
2016-07-21 13:54:38 +00:00
},
Err(e) => {
trace_error(&e);
None
2016-07-06 17:38:00 +00:00
}
2016-07-21 13:54:38 +00:00
})
.collect();
let outstring = if verbose {
let output = Command::new("task")
.stdin(Stdio::null())
.args(&uuids)
.spawn()
.unwrap_or_else(|e| {
trace_error(&e);
panic!("Failed to execute `task` on the commandline. I'm dying now.");
})
.wait_with_output()
.unwrap_or_else(|e| panic!("failed to unwrap output: {}", e));
String::from_utf8(output.stdout)
.unwrap_or_else(|e| panic!("failed to execute: {}", e))
} else {
uuids.join("\n")
};
println!("{}", outstring);
});
if let Err(e) = res {
trace_error(&e);
}
2016-07-06 17:38:00 +00:00
}
2016-06-01 14:05:43 +00:00