diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-06-06 19:54:19 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-06-09 15:41:42 +0200 |
commit | 19a37e6f9f053d8373d3e9bdba122c48262ded73 (patch) | |
tree | 9d6b7db291848eecc53ab7dbd9e06946b69f6564 /imag-diary | |
parent | 4a134144d124b7e2bb2812255ef00e44671730a9 (diff) |
Implement delete()
Diffstat (limited to 'imag-diary')
-rw-r--r-- | imag-diary/src/main.rs | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/imag-diary/src/main.rs b/imag-diary/src/main.rs index e93ea3f2..a3942051 100644 --- a/imag-diary/src/main.rs +++ b/imag-diary/src/main.rs @@ -55,7 +55,7 @@ fn main() { debug!("Call {}", name); match name { "create" => create(&rt), - // "delete" => delete(&rt), + "delete" => delete(&rt), "edit" => edit(&rt), "list" => list(&rt), "diary" => diary(&rt), @@ -135,7 +135,57 @@ fn list(rt: &Runtime) { } fn delete(rt: &Runtime) { - unimplemented!() + use libimaginteraction::ask::ask_bool; + + let diaryname = get_diary_name(rt); + if diaryname.is_none() { + warn!("No diary selected. Use either the configuration file or the commandline option"); + exit(1); + } + let diaryname = diaryname.unwrap(); + + 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, + + Some(Err(e)) => { + trace_error(&e); + exit(1); + }, + None => { + warn!("No entry"); + exit(1); + }, + }; + + if !ask_bool(&format!("Deleting {:?}", to_del.get_location())[..], Some(true)) { + info!("Aborting delete action"); + return; + } + + match diary.delete_entry(to_del) { + Ok(_) => info!("Ok"), + Err(e) => { + trace_error(&e); + exit(1); + }, + } } fn edit(rt: &Runtime) { |