Fix: Do not create entries with Store::retrieve()

This patch fixes a bug where entries where created with
`Store::retrieve()` rather than with the API from libimagdiary.
This caused headers to be missing.

Now, the CLI is parsed for the values passed and a NaiveDateTime object
is crafted from that, which is then passed to libimagdiary.
This commit is contained in:
Matthias Beyer 2018-06-08 02:20:00 +02:00
parent 22d63f0946
commit b896bc2657

View file

@ -20,6 +20,7 @@
use clap::ArgMatches; use clap::ArgMatches;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use chrono::Local; use chrono::Local;
use chrono::Timelike;
use libimagdiary::diary::Diary; use libimagdiary::diary::Diary;
use libimagdiary::error::DiaryErrorKind as DEK; use libimagdiary::error::DiaryErrorKind as DEK;
@ -73,8 +74,8 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock
} }
}) })
.map(|timed| { .map(|timed| {
let id = create_id_from_clispec(&create, &diaryname, timed); let time = create_id_from_clispec(&create, &diaryname, timed);
diary.retrieve(id).chain_err(|| DEK::StoreReadError) diary.new_entry_at(&diaryname, &time).chain_err(|| DEK::StoreWriteError)
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
debug!("Creating non-timed entry"); debug!("Creating non-timed entry");
@ -88,41 +89,31 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock
} }
fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Timed) -> DiaryId { fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Timed) -> NaiveDateTime {
use std::str::FromStr; use std::str::FromStr;
let get_hourly_id = |create: &ArgMatches| -> DiaryId { let dt = Local::now();
let time = DiaryId::now(String::from(diaryname)); let ndt = dt.naive_local();
let hr = create
.value_of("hour")
.map(|v| { debug!("Creating hourly entry with hour = {:?}", v); v })
.and_then(|s| {
FromStr::from_str(s)
.map_err(|_| warn!("Could not parse hour: '{}'", s))
.ok()
})
.unwrap_or(time.hour());
time.with_hour(hr)
};
match timed_type { match timed_type {
Timed::Daily => { Timed::Daily => {
debug!("Creating daily-timed entry"); debug!("Creating daily-timed entry");
get_hourly_id(create) ndt.with_hour(0)
.with_hour(0) .unwrap() // safe because hour = 0 is safe
.with_minute(0) .with_minute(0)
.unwrap() // safe because minute = 0 is safe
.with_second(0) .with_second(0)
.unwrap() // safe because second = 0 is safe
}, },
Timed::Hourly => { Timed::Hourly => {
debug!("Creating hourly-timed entry"); debug!("Creating hourly-timed entry");
get_hourly_id(create) ndt.with_minute(0)
.with_minute(0) .unwrap() // safe because minute = 0 is safe
.with_second(0) .with_second(0)
.unwrap() // safe because second = 0 is safe
}, },
Timed::Minutely => { Timed::Minutely => {
let time = get_hourly_id(create);
let min = create let min = create
.value_of("minute") .value_of("minute")
.map(|m| { debug!("minute = {:?}", m); m }) .map(|m| { debug!("minute = {:?}", m); m })
@ -133,12 +124,16 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time
}) })
.unwrap_or(ndt.minute()); .unwrap_or(ndt.minute());
time.with_minute(min) ndt.with_minute(min)
.unwrap_or_else(|| {
error!("Cannot set {} as minute, would yield invalid time!", min);
::std::process::exit(1)
})
.with_second(0) .with_second(0)
.unwrap() // safe because second = 0 is safe
}, },
Timed::Secondly => { Timed::Secondly => {
let time = get_hourly_id(create);
let min = create let min = create
.value_of("minute") .value_of("minute")
.map(|m| { debug!("minute = {:?}", m); m }) .map(|m| { debug!("minute = {:?}", m); m })
@ -159,7 +154,16 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Time
}) })
.unwrap_or(ndt.second()); .unwrap_or(ndt.second());
time.with_minute(min).with_second(sec) ndt.with_minute(min)
.unwrap_or_else(|| {
error!("Cannot set {} as minute, would yield invalid time!", min);
::std::process::exit(1)
})
.with_second(sec)
.unwrap_or_else(|| {
error!("Cannot set {} as second, would yield invalid time!", sec);
::std::process::exit(1)
})
}, },
} }
} }