Add kairos support in "list" subcommand

This patch adds kairos support in the "list" subcommand for the "-f" and
"-t" parameters which limit the entries to show.

Something like

    imag timetrack list --from yesterday

is now possible.
This commit is contained in:
Matthias Beyer 2018-02-11 21:25:33 +01:00
parent 4c95625def
commit cf0f93104d
4 changed files with 37 additions and 16 deletions

View file

@ -25,6 +25,7 @@ chrono = "0.4"
filters = "0.2"
itertools = "0.7"
prettytable-rs = "0.6"
kairos = "0.1"
libimagstore = { version = "0.7.0", path = "../../../lib/core/libimagstore" }
libimagrt = { version = "0.7.0", path = "../../../lib/core/libimagrt" }

View file

@ -17,13 +17,14 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::str::FromStr;
use chrono::NaiveDateTime;
use filters::filter::Filter;
use prettytable::Table;
use prettytable::row::Row;
use prettytable::cell::Cell;
use kairos::parser::Parsed;
use kairos::parser::parse as kairos_parse;
use clap::ArgMatches;
use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace;
@ -39,23 +40,38 @@ pub fn list(rt: &Runtime) -> i32 {
let (_, cmd) = rt.cli().subcommand();
let cmd = cmd.unwrap(); // checked in main()
let start = match cmd.value_of("start-time").map(::chrono::naive::NaiveDateTime::from_str) {
None => None,
Some(Ok(dt)) => Some(dt),
Some(Err(e)) => {
trace_error(&e);
None
}
};
let end = match cmd.value_of("end-time").map(::chrono::naive::NaiveDateTime::from_str) {
None => None,
Some(Ok(dt)) => Some(dt),
Some(Err(e)) => {
trace_error(&e);
None
let gettime = |cmd: &ArgMatches, name| {
match cmd.value_of(name).map(kairos_parse) {
Some(Ok(Parsed::TimeType(tt))) => match tt.calculate() {
Ok(tt) => {
let dt = tt.get_moment().unwrap_or_else(|| {
error!("Failed to get date from '{}'", cmd.value_of(name).unwrap());
::std::process::exit(1)
});
Some(dt.clone())
},
Err(e) => {
error!("Failed to calculate date from '{}': {:?}",
cmd.value_of(name).unwrap(), e);
::std::process::exit(1)
},
},
Some(Ok(Parsed::Iterator(_))) => {
error!("Expected single point in time, got '{}', which yields a list of dates", cmd.value_of(name).unwrap());
::std::process::exit(1)
},
Some(Err(e)) => {
trace_error(&e);
::std::process::exit(1)
}
None => None,
}
};
let start = gettime(&cmd, "start-time");
let end = gettime(&cmd, "end-time");
let list_not_ended = cmd.is_present("list-not-ended");
list_impl(rt, start, end, list_not_ended)

View file

@ -25,6 +25,7 @@ extern crate chrono;
extern crate filters;
extern crate itertools;
extern crate prettytable;
extern crate kairos;
extern crate libimagerror;
extern crate libimagstore;

View file

@ -19,6 +19,9 @@ Version 0.y.z and thus we can break the API like we want and need to.
This section contains the changelog from the last release to the next release.
* Major changes
* `imag-timetrack list --from/--to` now have `kairos` support - that means
that complex `--from/--to` arguments (like `yesterday` or `today-2weeks`)
are now possible
* Minor changes
* Bugfixes