2016-06-08 17:40:13 +00:00
|
|
|
use std::process::exit;
|
|
|
|
use chrono::naive::datetime::NaiveDateTime;
|
|
|
|
|
|
|
|
use libimagdiary::diary::Diary;
|
|
|
|
use libimagdiary::diaryid::DiaryId;
|
|
|
|
use libimagrt::runtime::Runtime;
|
2016-07-15 19:56:53 +00:00
|
|
|
use libimagerror::trace::trace_error_exit;
|
2016-06-08 17:40:13 +00:00
|
|
|
use libimagtimeui::datetime::DateTime;
|
|
|
|
use libimagtimeui::parse::Parse;
|
|
|
|
|
|
|
|
use util::get_diary_name;
|
|
|
|
|
|
|
|
pub fn delete(rt: &Runtime) {
|
|
|
|
use libimaginteraction::ask::ask_bool;
|
|
|
|
|
2016-09-04 15:45:46 +00:00
|
|
|
let diaryname = get_diary_name(rt).unwrap_or_else(|| {
|
2016-06-08 17:40:13 +00:00
|
|
|
warn!("No diary selected. Use either the configuration file or the commandline option");
|
|
|
|
exit(1);
|
2016-09-04 15:45:46 +00:00
|
|
|
});
|
2016-06-08 17:40:13 +00:00
|
|
|
|
|
|
|
let diary = Diary::open(rt.store(), &diaryname[..]);
|
|
|
|
debug!("Diary opened: {:?}", diary);
|
|
|
|
|
|
|
|
let datetime : Option<NaiveDateTime> = rt
|
|
|
|
.cli()
|
|
|
|
.subcommand_matches("delete")
|
|
|
|
.unwrap()
|
|
|
|
.value_of("datetime")
|
|
|
|
.map(|dt| { debug!("DateTime = {:?}", dt); dt })
|
|
|
|
.and_then(DateTime::parse)
|
|
|
|
.map(|dt| dt.into());
|
|
|
|
|
|
|
|
let to_del = match datetime {
|
|
|
|
Some(dt) => Some(diary.retrieve(DiaryId::from_datetime(diaryname.clone(), dt))),
|
|
|
|
None => diary.get_youngest_entry(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let to_del = match to_del {
|
|
|
|
Some(Ok(e)) => e,
|
|
|
|
|
2016-07-15 19:56:53 +00:00
|
|
|
Some(Err(e)) => trace_error_exit(&e, 1),
|
2016-06-08 17:40:13 +00:00
|
|
|
None => {
|
|
|
|
warn!("No entry");
|
|
|
|
exit(1);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if !ask_bool(&format!("Deleting {:?}", to_del.get_location())[..], Some(true)) {
|
|
|
|
info!("Aborting delete action");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:46:32 +00:00
|
|
|
if let Err(e) = diary.delete_entry(to_del) {
|
|
|
|
trace_error_exit(&e, 1)
|
2016-06-08 17:40:13 +00:00
|
|
|
}
|
2016-09-04 15:46:32 +00:00
|
|
|
|
|
|
|
info!("Ok!");
|
2016-06-08 17:40:13 +00:00
|
|
|
}
|
|
|
|
|