2016-05-10 13:54:46 +00:00
|
|
|
extern crate clap;
|
|
|
|
extern crate glob;
|
|
|
|
#[macro_use] extern crate log;
|
2016-06-28 18:13:11 +00:00
|
|
|
extern crate serde_json;
|
2016-05-10 13:54:46 +00:00
|
|
|
extern crate semver;
|
|
|
|
extern crate toml;
|
|
|
|
#[macro_use] extern crate version;
|
|
|
|
|
2016-06-28 18:13:11 +00:00
|
|
|
extern crate task_hookrs;
|
|
|
|
|
2016-05-10 13:54:46 +00:00
|
|
|
extern crate libimagrt;
|
|
|
|
extern crate libimagstore;
|
2016-07-04 12:04:11 +00:00
|
|
|
extern crate libimagerror;
|
2016-06-28 18:13:11 +00:00
|
|
|
extern crate libimagtodo;
|
2016-05-10 13:54:46 +00:00
|
|
|
|
|
|
|
use std::process::exit;
|
2016-06-07 21:45:49 +00:00
|
|
|
use std::process::{Command, Stdio};
|
2016-06-28 18:13:11 +00:00
|
|
|
use std::io::stdin;
|
2016-06-29 19:37:21 +00:00
|
|
|
use std::io::BufRead;
|
2016-06-28 18:13:11 +00:00
|
|
|
|
2016-06-30 17:02:12 +00:00
|
|
|
use task_hookrs::import::{import_task, import_tasks};
|
2016-07-15 19:24:49 +00:00
|
|
|
use task_hookrs::status::TaskStatus;
|
2016-05-10 13:54:46 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-07-08 19:49:23 +00:00
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2016-06-28 18:13:11 +00:00
|
|
|
use libimagtodo::task::IntoTask;
|
2016-07-07 14:20:00 +00:00
|
|
|
use libimagtodo::task::Task;
|
2016-07-04 12:04:11 +00:00
|
|
|
use libimagerror::trace::trace_error;
|
2016-05-10 13:54:46 +00:00
|
|
|
|
|
|
|
mod ui;
|
|
|
|
|
|
|
|
use ui::build_ui;
|
2016-04-20 17:23:35 +00:00
|
|
|
fn main() {
|
2016-07-08 19:49:23 +00:00
|
|
|
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") {
|
2016-07-06 17:50:55 +00:00
|
|
|
let stdin = stdin();
|
2016-07-06 17:38:00 +00:00
|
|
|
let mut stdin = stdin.lock();
|
2016-07-06 17:50:55 +00:00
|
|
|
|
2016-07-10 14:35:24 +00:00
|
|
|
match Task::import(rt.store(), stdin) {
|
|
|
|
Ok((_, uuid)) => info!("Task {} stored in imag", uuid),
|
|
|
|
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();
|
|
|
|
|
2016-07-06 17:53:47 +00:00
|
|
|
match import_tasks(stdin) {
|
2016-07-15 19:24:49 +00:00
|
|
|
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-07-15 19:24:49 +00:00
|
|
|
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-15 19:24:49 +00:00
|
|
|
}
|
2016-07-06 17:38:00 +00:00
|
|
|
} // end if c % 2
|
2016-07-06 17:53:47 +00:00
|
|
|
},
|
|
|
|
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-06 17:50:55 +00:00
|
|
|
let subcmd = rt.cli().subcommand_matches("list").unwrap();
|
2016-07-06 17:38:00 +00:00
|
|
|
let mut args = Vec::new();
|
2016-07-06 17:50:55 +00:00
|
|
|
let verbose = subcmd.is_present("verbose");
|
2016-07-07 14:20:00 +00:00
|
|
|
let iter = match Task::all(rt.store()) {
|
|
|
|
Ok(iter) => iter,
|
|
|
|
Err(e) => {
|
2016-07-06 17:50:55 +00:00
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
2016-07-07 14:20:00 +00:00
|
|
|
},
|
2016-07-06 17:38:00 +00:00
|
|
|
};
|
2016-07-06 17:50:55 +00:00
|
|
|
|
2016-07-06 17:38:00 +00:00
|
|
|
for task in iter {
|
|
|
|
match task {
|
|
|
|
Ok(val) => {
|
2016-07-07 14:20:00 +00:00
|
|
|
let uuid = match val.get_header().read("todo.uuid") {
|
2016-07-06 17:38:00 +00:00
|
|
|
Ok(Some(u)) => u,
|
2016-07-06 17:50:55 +00:00
|
|
|
Ok(None) => continue,
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
2016-06-28 21:05:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-07-06 17:38:00 +00:00
|
|
|
};
|
2016-07-06 17:50:55 +00:00
|
|
|
|
2016-07-06 17:38:00 +00:00
|
|
|
if verbose {
|
|
|
|
args.clear();
|
2016-07-06 17:56:41 +00:00
|
|
|
args.push(format!("uuid:{} information", uuid));
|
2016-07-06 17:57:49 +00:00
|
|
|
|
|
|
|
let tw_process = Command::new("task")
|
|
|
|
.stdin(Stdio::null())
|
|
|
|
.args(&args)
|
|
|
|
.spawn()
|
2016-07-06 17:38:00 +00:00
|
|
|
.unwrap_or_else(|e| {
|
2016-07-06 17:50:55 +00:00
|
|
|
trace_error(&e);
|
2016-07-06 17:38:00 +00:00
|
|
|
panic!("failed");
|
|
|
|
});
|
2016-07-06 17:57:49 +00:00
|
|
|
let output = tw_process
|
|
|
|
.wait_with_output()
|
|
|
|
.unwrap_or_else(|e| panic!("failed to unwrap output: {}", e));
|
|
|
|
let outstring = String::from_utf8(output.stdout)
|
|
|
|
.unwrap_or_else(|e| panic!("failed to execute: {}", e));
|
|
|
|
|
2016-07-06 17:38:00 +00:00
|
|
|
println!("{}", outstring);
|
2016-07-06 17:50:55 +00:00
|
|
|
} else {
|
2016-07-06 17:38:00 +00:00
|
|
|
println!("{}", match uuid {
|
|
|
|
toml::Value::String(s) => s,
|
|
|
|
_ => {
|
|
|
|
error!("Unexpected type for todo.uuid: {}", uuid);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2016-07-06 17:50:55 +00:00
|
|
|
trace_error(&e);
|
2016-07-06 17:38:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} // end match task
|
|
|
|
} // end for
|
|
|
|
}
|
2016-06-01 14:05:43 +00:00
|
|
|
|