2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
|
2018-09-27 06:13:58 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2018-04-13 22:39:43 +00:00
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
extern crate clap;
|
|
|
|
#[macro_use] extern crate log;
|
|
|
|
extern crate toml;
|
2017-05-30 19:09:52 +00:00
|
|
|
extern crate toml_query;
|
|
|
|
#[macro_use] extern crate is_match;
|
2018-10-30 17:40:53 +00:00
|
|
|
extern crate failure;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2017-05-30 18:25:00 +00:00
|
|
|
extern crate libimagerror;
|
|
|
|
extern crate libimagtodo;
|
|
|
|
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
use std::io::stdin;
|
2018-02-14 17:44:25 +00:00
|
|
|
use std::io::Write;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
2017-09-02 19:48:31 +00:00
|
|
|
use libimagtodo::taskstore::TaskStore;
|
2018-02-11 12:49:45 +00:00
|
|
|
use libimagerror::trace::{MapErrTrace, trace_error};
|
2018-04-30 17:24:05 +00:00
|
|
|
use libimagerror::iter::TraceIterator;
|
2018-02-14 17:44:25 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
|
|
|
use libimagerror::io::ToExitCode;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
mod ui;
|
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::ui::build_ui;
|
2017-05-30 18:25:00 +00:00
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2017-05-30 18:25:00 +00:00
|
|
|
let rt = generate_runtime_setup("imag-todo",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2017-05-30 18:25:00 +00:00
|
|
|
"Interface with taskwarrior",
|
|
|
|
build_ui);
|
|
|
|
|
|
|
|
match rt.cli().subcommand_name() {
|
|
|
|
Some("tw-hook") => tw_hook(&rt),
|
|
|
|
Some("list") => list(&rt),
|
2018-04-04 15:01:02 +00:00
|
|
|
Some(other) => {
|
|
|
|
debug!("Unknown command");
|
|
|
|
let _ = rt.handle_unknown_subcommand("imag-todo", other, rt.cli())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-04-04 15:01:02 +00:00
|
|
|
.code()
|
2018-04-13 22:57:15 +00:00
|
|
|
.map(::std::process::exit);
|
2018-04-04 15:01:02 +00:00
|
|
|
}
|
2017-05-30 18:25:00 +00:00
|
|
|
None => {
|
|
|
|
warn!("No command");
|
|
|
|
},
|
|
|
|
} // 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();
|
2017-09-04 14:24:50 +00:00
|
|
|
|
|
|
|
// implements BufRead which is required for `Store::import_task_from_reader()`
|
|
|
|
let stdin = stdin.lock();
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2018-02-11 12:49:45 +00:00
|
|
|
let (_, line, uuid ) = rt
|
|
|
|
.store()
|
|
|
|
.import_task_from_reader(stdin)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2018-02-11 12:49:45 +00:00
|
|
|
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = writeln!(rt.stdout(), "{}\nTask {} stored in imag", line, uuid)
|
2018-02-14 17:44:25 +00:00
|
|
|
.to_exit_code()
|
|
|
|
.unwrap_or_exit();
|
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
} else if subcmd.is_present("delete") {
|
|
|
|
// The used hook is "on-modify". This hook gives two json-objects
|
|
|
|
// per usage und wants one (the second one) back.
|
|
|
|
let stdin = stdin();
|
2017-09-02 12:34:03 +00:00
|
|
|
rt.store().delete_tasks_by_imports(stdin.lock()).map_err_trace().ok();
|
2017-05-30 18:25:00 +00:00
|
|
|
} else {
|
|
|
|
// Should not be possible, as one argument is required via
|
|
|
|
// ArgGroup
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list(rt: &Runtime) {
|
2018-01-12 15:30:33 +00:00
|
|
|
use toml_query::read::TomlValueReadTypeExt;
|
2017-05-30 19:09:52 +00:00
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
let subcmd = rt.cli().subcommand_matches("list").unwrap();
|
|
|
|
let verbose = subcmd.is_present("verbose");
|
|
|
|
|
2017-05-30 19:09:52 +00:00
|
|
|
// Helper for toml_query::read::TomlValueReadExt::read() return value, which does only
|
|
|
|
// return Result<T> instead of Result<Option<T>>, which is a real inconvenience.
|
|
|
|
//
|
|
|
|
let no_identifier = |e: &::toml_query::error::Error| -> bool {
|
2018-10-30 17:40:53 +00:00
|
|
|
is_match!(e, &::toml_query::error::Error::IdentifierNotFoundInDocument(_))
|
2017-05-30 19:09:52 +00:00
|
|
|
};
|
|
|
|
|
2017-09-02 12:34:03 +00:00
|
|
|
let res = rt.store().all_tasks() // get all tasks
|
2017-05-30 18:25:00 +00:00
|
|
|
.map(|iter| { // and if this succeeded
|
|
|
|
// filter out the ones were we can read the uuid
|
2019-02-05 00:37:32 +00:00
|
|
|
let uuids : Vec<_> = iter.trace_unwrap_exit().filter_map(|storeid| {
|
2017-09-02 11:29:49 +00:00
|
|
|
match rt.store().retrieve(storeid) {
|
|
|
|
Ok(fle) => {
|
2018-01-12 15:30:33 +00:00
|
|
|
match fle.get_header().read_string("todo.uuid") {
|
|
|
|
Ok(Some(ref u)) => Some(u.clone()),
|
2017-09-02 11:29:49 +00:00
|
|
|
Ok(None) => {
|
2017-09-04 14:24:50 +00:00
|
|
|
error!("Header missing field in {}", fle.get_location());
|
2017-09-02 11:29:49 +00:00
|
|
|
None
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
if !no_identifier(&e) {
|
2018-10-30 17:40:53 +00:00
|
|
|
trace_error(&Error::from(e));
|
2017-09-02 11:29:49 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 19:45:13 +00:00
|
|
|
},
|
2017-05-30 18:25:00 +00:00
|
|
|
Err(e) => {
|
2017-09-02 11:29:49 +00:00
|
|
|
trace_error(&e);
|
2017-05-30 18:25:00 +00:00
|
|
|
None
|
2017-09-02 11:29:49 +00:00
|
|
|
},
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// compose a `task` call with them, ...
|
|
|
|
let outstring = if verbose { // ... if verbose
|
|
|
|
let output = Command::new("task")
|
|
|
|
.stdin(Stdio::null())
|
|
|
|
.args(&uuids)
|
|
|
|
.spawn()
|
|
|
|
.unwrap_or_else(|e| {
|
2018-02-11 12:49:45 +00:00
|
|
|
error!("Failed to execute `task` on the commandline: {:?}. I'm dying now.", e);
|
|
|
|
::std::process::exit(1)
|
2017-05-30 18:25:00 +00:00
|
|
|
})
|
|
|
|
.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))
|
|
|
|
} else { // ... else just join them
|
|
|
|
uuids.join("\n")
|
|
|
|
};
|
|
|
|
|
|
|
|
// and then print that
|
2018-02-26 21:57:17 +00:00
|
|
|
let _ = writeln!(rt.stdout(), "{}", outstring).to_exit_code().unwrap_or_exit();
|
2017-05-30 18:25:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
res.map_err_trace().ok();
|
|
|
|
}
|
|
|
|
|