imag/imag-todo/src/main.rs

139 lines
4.6 KiB
Rust
Raw Normal View History

//
// 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
//
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::{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;
use libimagrt::runtime::Runtime;
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};
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),
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") {
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