2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2018-02-07 01:48:53 +00:00
|
|
|
// Copyright (C) 2015-2018 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2017-08-29 14:26:57 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagdiary::diary::Diary;
|
|
|
|
use libimagdiary::diaryid::DiaryId;
|
|
|
|
use libimagdiary::error::DiaryErrorKind as DEK;
|
2017-09-03 18:45:23 +00:00
|
|
|
use libimagdiary::error::ResultExt;
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagentryedit::edit::Edit;
|
|
|
|
use libimagrt::runtime::Runtime;
|
2017-09-14 16:59:03 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagutil::warn_exit::warn_exit;
|
2017-08-29 14:04:11 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::store::Store;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
use util::get_diary_name;
|
2017-09-14 16:59:03 +00:00
|
|
|
use util::get_diary_timed_config;
|
|
|
|
use util::Timed;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
pub fn create(rt: &Runtime) {
|
|
|
|
let diaryname = get_diary_name(rt)
|
|
|
|
.unwrap_or_else( || warn_exit("No diary selected. Use either the configuration file or the commandline option", 1));
|
|
|
|
|
2017-08-29 14:04:11 +00:00
|
|
|
let mut entry = create_entry(rt.store(), &diaryname, rt);
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2017-08-29 14:26:57 +00:00
|
|
|
let res = if rt.cli().subcommand_matches("create").unwrap().is_present("no-edit") {
|
2017-08-29 14:04:11 +00:00
|
|
|
debug!("Not editing new diary entry");
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
debug!("Editing new diary entry");
|
2017-08-29 14:26:57 +00:00
|
|
|
entry.edit_content(rt)
|
2017-09-04 21:02:45 +00:00
|
|
|
.chain_err(|| DEK::DiaryEditError)
|
2017-08-29 14:04:11 +00:00
|
|
|
};
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2018-02-08 14:19:29 +00:00
|
|
|
let _ = res.map_err_trace_exit_unwrap(1);
|
|
|
|
info!("Ok!");
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:26:57 +00:00
|
|
|
fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLockEntry<'a> {
|
2017-09-14 16:59:03 +00:00
|
|
|
use util::parse_timed_string;
|
|
|
|
|
2017-08-29 14:26:57 +00:00
|
|
|
let create = rt.cli().subcommand_matches("create").unwrap();
|
2017-09-14 16:59:03 +00:00
|
|
|
|
2018-02-08 14:20:38 +00:00
|
|
|
create.value_of("timed")
|
2017-10-12 17:08:36 +00:00
|
|
|
.map(|t| parse_timed_string(t, diaryname).map_err_trace_exit_unwrap(1))
|
2017-09-14 16:59:03 +00:00
|
|
|
.map(Some)
|
2018-02-04 18:50:05 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
match get_diary_timed_config(rt, diaryname).map_err_trace_exit_unwrap(1) {
|
|
|
|
Some(t) => Some(t),
|
|
|
|
None => {
|
|
|
|
warn!("Missing config: 'diary.diaries.{}.timed'", diaryname);
|
|
|
|
warn!("Assuming 'false'");
|
|
|
|
None
|
|
|
|
}
|
2017-09-14 16:59:03 +00:00
|
|
|
}
|
2018-02-08 14:20:38 +00:00
|
|
|
})
|
|
|
|
.map(|timed| {
|
2017-09-14 16:59:03 +00:00
|
|
|
let id = create_id_from_clispec(&create, &diaryname, timed);
|
|
|
|
diary.retrieve(id).chain_err(|| DEK::StoreReadError)
|
2018-02-08 14:20:38 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
2017-09-14 16:59:03 +00:00
|
|
|
debug!("Creating non-timed entry");
|
|
|
|
diary.new_entry_today(diaryname)
|
2018-02-08 14:20:38 +00:00
|
|
|
})
|
|
|
|
.map(|e| {
|
|
|
|
debug!("Created: {}", e.get_location());
|
|
|
|
e
|
|
|
|
})
|
|
|
|
.map_err_trace_exit_unwrap(1)
|
2017-08-29 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-14 16:59:03 +00:00
|
|
|
fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Timed) -> DiaryId {
|
2017-08-29 14:26:57 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
let get_hourly_id = |create: &ArgMatches| -> DiaryId {
|
|
|
|
let time = DiaryId::now(String::from(diaryname));
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2017-09-14 16:59:03 +00:00
|
|
|
match timed_type {
|
|
|
|
Timed::Hourly => {
|
2017-08-29 14:26:57 +00:00
|
|
|
debug!("Creating hourly-timed entry");
|
|
|
|
get_hourly_id(create)
|
|
|
|
},
|
|
|
|
|
2017-09-14 16:59:03 +00:00
|
|
|
Timed::Minutely => {
|
2017-08-29 14:26:57 +00:00
|
|
|
let time = get_hourly_id(create);
|
|
|
|
let min = create
|
|
|
|
.value_of("minute")
|
|
|
|
.map(|m| { debug!("minute = {:?}", m); m })
|
|
|
|
.and_then(|s| {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
.map_err(|_| warn!("Could not parse minute: '{}'", s))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.unwrap_or(time.minute());
|
|
|
|
|
|
|
|
time.with_minute(min)
|
|
|
|
},
|
2018-02-04 18:50:05 +00:00
|
|
|
|
|
|
|
Timed::Secondly => {
|
|
|
|
let time = get_hourly_id(create);
|
|
|
|
let min = create
|
|
|
|
.value_of("minute")
|
|
|
|
.map(|m| { debug!("minute = {:?}", m); m })
|
|
|
|
.and_then(|s| {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
.map_err(|_| warn!("Could not parse minute: '{}'", s))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.unwrap_or(time.minute());
|
|
|
|
|
|
|
|
let sec = create
|
|
|
|
.value_of("second")
|
|
|
|
.map(|s| { debug!("second = {:?}", s); s })
|
|
|
|
.and_then(|s| {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
.map_err(|_| warn!("Could not parse second: '{}'", s))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.unwrap_or(time.second());
|
|
|
|
|
|
|
|
time.with_minute(min).with_second(sec)
|
|
|
|
},
|
2017-08-29 14:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|