imag/imag-todo/src/main.rs

192 lines
6.0 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-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};
use libimagrt::runtime::Runtime;
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;
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),
_ => 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();
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 => {
2016-07-07 14:20:00 +00:00
match Task::delete_by_uuid(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;
},
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();
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