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-05-10 13:54:46 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-06-28 18:13:11 +00:00
|
|
|
use libimagtodo::task::IntoTask;
|
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-06 17:57:49 +00:00
|
|
|
let name = "imag-todo";
|
2016-05-10 14:28:35 +00:00
|
|
|
let version = &version!()[..];
|
2016-07-06 17:57:49 +00:00
|
|
|
let about = "Interface with taskwarrior";
|
|
|
|
let ui = build_ui(Runtime::get_default_cli_builder(name, version, about));
|
2016-06-28 18:13:11 +00:00
|
|
|
|
2016-05-10 14:28:35 +00:00
|
|
|
let rt = {
|
|
|
|
let rt = Runtime::new(ui);
|
|
|
|
if rt.is_ok() {
|
|
|
|
rt.unwrap()
|
|
|
|
} else {
|
|
|
|
println!("Could not set up Runtime");
|
|
|
|
println!("{:?}", rt.unwrap_err());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
} // 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
|
|
|
let mut line = String::new();
|
|
|
|
|
|
|
|
if let Err(e) = stdin.read_line(&mut line) {
|
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
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 let Ok(ttask) = import_task(&line.as_str()) {
|
2016-07-06 17:50:55 +00:00
|
|
|
match serde_json::ser::to_string(&ttask) {
|
|
|
|
Ok(val) => println!("{}", val),
|
2016-07-06 17:38:00 +00:00
|
|
|
Err(e) => {
|
2016-07-06 17:50:55 +00:00
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
2016-07-06 17:38:00 +00:00
|
|
|
}
|
2016-07-06 17:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let uuid = *ttask.uuid();
|
2016-07-06 18:14:26 +00:00
|
|
|
match ttask.into_task(rt.store()) {
|
2016-07-06 17:38:00 +00:00
|
|
|
Ok(val) => {
|
|
|
|
println!("Task {} stored in imag", uuid);
|
|
|
|
val
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
trace_error(&e);
|
2016-07-06 17:50:55 +00:00
|
|
|
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
|
|
|
error!("No usable input");
|
2016-07-06 17:50:55 +00:00
|
|
|
exit(1);
|
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 mut counter = 0;
|
|
|
|
let stdin = stdin();
|
|
|
|
let stdin = stdin.lock();
|
|
|
|
|
2016-07-06 17:53:47 +00:00
|
|
|
match import_tasks(stdin) {
|
|
|
|
Ok(ttasks) => for ttask in ttasks {
|
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-06 17:38:00 +00:00
|
|
|
match ttask.status() {
|
|
|
|
&task_hookrs::status::TaskStatus::Deleted => {
|
|
|
|
match libimagtodo::delete::delete(rt.store(), *ttask.uuid()) {
|
2016-07-06 17:50:55 +00:00
|
|
|
Ok(_) => println!("Deleted task {}", *ttask.uuid()),
|
2016-06-30 16:38:03 +00:00
|
|
|
Err(e) => {
|
2016-07-06 17:38:00 +00:00
|
|
|
trace_error(&e);
|
2016-07-06 17:50:55 +00:00
|
|
|
exit(1);
|
2016-06-30 16:38:03 +00:00
|
|
|
}
|
2016-06-28 21:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-06 17:38:00 +00:00
|
|
|
_ => {
|
2016-06-28 21:05:05 +00:00
|
|
|
}
|
2016-07-06 17:38:00 +00:00
|
|
|
} // end match ttask.status()
|
|
|
|
} // end if c % 2
|
|
|
|
counter += 1;
|
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");
|
|
|
|
let iter = match libimagtodo::read::get_todo_iterator(rt.store()) {
|
2016-07-06 17:38:00 +00:00
|
|
|
Err(e) => {
|
2016-07-06 17:50:55 +00:00
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
2016-07-06 17:38:00 +00:00
|
|
|
}
|
|
|
|
Ok(val) => val,
|
|
|
|
};
|
2016-07-06 17:50:55 +00:00
|
|
|
|
2016-07-06 17:38:00 +00:00
|
|
|
for task in iter {
|
|
|
|
match task {
|
|
|
|
Ok(val) => {
|
|
|
|
let uuid = match val.flentry.get_header().read("todo.uuid") {
|
|
|
|
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
|
|
|
|