2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
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
|
|
|
|
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-07-21 13:54:38 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
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-07-07 14:20:00 +00:00
|
|
|
use libimagtodo::task::Task;
|
2016-09-06 09:13:08 +00:00
|
|
|
use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
|
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-08-06 09:54:20 +00:00
|
|
|
None => {
|
|
|
|
warn!("No command");
|
|
|
|
},
|
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-15 19:27:28 +00:00
|
|
|
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) {
|
2016-08-06 09:50:50 +00:00
|
|
|
Ok((_, line, uuid)) => println!("{}\nTask {} stored in imag", line, uuid),
|
2016-09-06 09:13:08 +00:00
|
|
|
Err(e) => trace_error_exit(&e, 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 stdin = stdin();
|
2016-09-06 09:13:08 +00:00
|
|
|
Task::delete_by_imports(rt.store(), stdin.lock()).map_err_trace().ok();
|
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");
|
|
|
|
|
2016-08-06 08:17:26 +00:00
|
|
|
let res = Task::all(rt.store()) // get all tasks
|
|
|
|
.map(|iter| { // and if this succeeded
|
|
|
|
// filter out the ones were we can read the uuid
|
2016-07-21 13:54:38 +00:00
|
|
|
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();
|
|
|
|
|
2016-08-06 08:17:26 +00:00
|
|
|
// compose a `task` call with them, ...
|
|
|
|
let outstring = if verbose { // ... if verbose
|
2016-07-21 13:54:38 +00:00
|
|
|
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))
|
2016-08-06 08:17:26 +00:00
|
|
|
} else { // ... else just join them
|
2016-07-21 13:54:38 +00:00
|
|
|
uuids.join("\n")
|
|
|
|
};
|
|
|
|
|
2016-08-06 09:08:57 +00:00
|
|
|
// and then print that
|
2016-07-21 13:54:38 +00:00
|
|
|
println!("{}", outstring);
|
|
|
|
});
|
|
|
|
|
2016-09-06 09:13:08 +00:00
|
|
|
res.map_err_trace().ok();
|
2016-07-06 17:38:00 +00:00
|
|
|
}
|
2016-06-01 14:05:43 +00:00
|
|
|
|