use std::str::FromStr;
use chrono::naive::NaiveDateTime;
use failure::Error;
use failure::Fallible as Result;
use libimagrt::runtime::Runtime;
use libimagtimetrack::tag::TimeTrackingTag;
use libimagtimetrack::store::TimeTrackStore;
pub fn start(rt: &Runtime) -> Result<()> {
let (_, cmd) = rt.cli().subcommand();
let cmd = cmd.unwrap();
let start = {
let startstr = cmd.value_of("start-time").unwrap(); if startstr == "now" {
Ok(::chrono::offset::Local::now().naive_local())
} else {
NaiveDateTime::from_str(startstr)
}
}?;
cmd.values_of("tags")
.unwrap() .map(String::from)
.map(TimeTrackingTag::from)
.map(|ttt| {
let entry = rt.store().create_timetracking_at(&start, &ttt)?;
rt.report_touched(entry.get_location()).map_err(Error::from)
})
.collect::<Result<Vec<_>>>()
.map(|_| ())
}