2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 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;
|
2018-06-08 00:19:55 +00:00
|
|
|
use chrono::NaiveDateTime;
|
|
|
|
use chrono::Local;
|
2018-06-08 00:20:00 +00:00
|
|
|
use chrono::Timelike;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::ResultExt;
|
|
|
|
use failure::err_msg;
|
2017-08-29 14:26:57 +00:00
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagdiary::diary::Diary;
|
|
|
|
use libimagentryedit::edit::Edit;
|
|
|
|
use libimagrt::runtime::Runtime;
|
2017-09-14 16:59:03 +00:00
|
|
|
use libimagerror::trace::MapErrTrace;
|
2019-02-05 00:37:32 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagutil::warn_exit::warn_exit;
|
2018-11-08 14:52:03 +00:00
|
|
|
use libimagutil::debug_result::DebugResult;
|
|
|
|
use libimagutil::debug_option::DebugOption;
|
2017-08-29 14:04:11 +00:00
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagstore::store::Store;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::util::get_diary_name;
|
|
|
|
use crate::util::get_diary_timed_config;
|
|
|
|
use crate::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
|
|
|
|
2019-02-03 18:53:50 +00:00
|
|
|
let _ = rt.report_touched(entry.get_location()).unwrap_or_exit();
|
2018-10-06 10:50:20 +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");
|
2018-10-30 17:40:53 +00:00
|
|
|
entry.edit_content(rt).context(err_msg("Diary edit error")).map_err(Error::from)
|
2017-08-29 14:04:11 +00:00
|
|
|
};
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2019-02-05 00:37:32 +00:00
|
|
|
let _ = res.map_err_trace_exit_unwrap();
|
2018-02-08 14:19:29 +00:00
|
|
|
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> {
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::util::parse_timed_string;
|
2017-09-14 16:59:03 +00:00
|
|
|
|
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")
|
2019-02-05 00:37:32 +00:00
|
|
|
.map(|t| parse_timed_string(t, diaryname).map_err_trace_exit_unwrap())
|
2017-09-14 16:59:03 +00:00
|
|
|
.map(Some)
|
2018-02-04 18:50:05 +00:00
|
|
|
.unwrap_or_else(|| {
|
2019-02-05 00:37:32 +00:00
|
|
|
match get_diary_timed_config(rt, diaryname).map_err_trace_exit_unwrap() {
|
2018-02-04 18:50:05 +00:00
|
|
|
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| {
|
2018-11-08 14:43:00 +00:00
|
|
|
let time = create_id_from_clispec(&create, timed);
|
2018-10-30 17:40:53 +00:00
|
|
|
diary.new_entry_at(&diaryname, &time)
|
|
|
|
.context(err_msg("Store write error"))
|
|
|
|
.map_err(Error::from)
|
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
|
|
|
})
|
2018-11-08 14:52:03 +00:00
|
|
|
.map_dbg(|e| format!("Created: {}", e.get_location()))
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2017-08-29 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-08 14:43:00 +00:00
|
|
|
fn create_id_from_clispec(create: &ArgMatches, timed_type: Timed) -> NaiveDateTime {
|
2017-08-29 14:26:57 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2018-06-08 00:20:00 +00:00
|
|
|
let dt = Local::now();
|
|
|
|
let ndt = dt.naive_local();
|
2017-08-29 14:26:57 +00:00
|
|
|
|
2017-09-14 16:59:03 +00:00
|
|
|
match timed_type {
|
2018-03-02 13:37:44 +00:00
|
|
|
Timed::Daily => {
|
|
|
|
debug!("Creating daily-timed entry");
|
2018-06-08 00:20:00 +00:00
|
|
|
ndt.with_hour(0)
|
|
|
|
.unwrap() // safe because hour = 0 is safe
|
2018-03-02 13:37:44 +00:00
|
|
|
.with_minute(0)
|
2018-06-08 00:20:00 +00:00
|
|
|
.unwrap() // safe because minute = 0 is safe
|
2018-03-02 13:37:44 +00:00
|
|
|
.with_second(0)
|
2018-06-08 00:20:00 +00:00
|
|
|
.unwrap() // safe because second = 0 is safe
|
2018-03-02 13:37:44 +00:00
|
|
|
},
|
2017-09-14 16:59:03 +00:00
|
|
|
Timed::Hourly => {
|
2017-08-29 14:26:57 +00:00
|
|
|
debug!("Creating hourly-timed entry");
|
2018-06-08 00:20:00 +00:00
|
|
|
ndt.with_minute(0)
|
|
|
|
.unwrap() // safe because minute = 0 is safe
|
2018-03-02 13:37:36 +00:00
|
|
|
.with_second(0)
|
2018-06-08 00:20:00 +00:00
|
|
|
.unwrap() // safe because second = 0 is safe
|
2017-08-29 14:26:57 +00:00
|
|
|
},
|
|
|
|
|
2017-09-14 16:59:03 +00:00
|
|
|
Timed::Minutely => {
|
2017-08-29 14:26:57 +00:00
|
|
|
let min = create
|
|
|
|
.value_of("minute")
|
2018-11-08 14:52:03 +00:00
|
|
|
.map_dbg(|m| format!("minute = {:?}", m))
|
2017-08-29 14:26:57 +00:00
|
|
|
.and_then(|s| {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
.map_err(|_| warn!("Could not parse minute: '{}'", s))
|
|
|
|
.ok()
|
|
|
|
})
|
2018-06-08 00:19:55 +00:00
|
|
|
.unwrap_or(ndt.minute());
|
2017-08-29 14:26:57 +00:00
|
|
|
|
2018-06-08 00:20:00 +00:00
|
|
|
ndt.with_minute(min)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("Cannot set {} as minute, would yield invalid time!", min);
|
|
|
|
::std::process::exit(1)
|
|
|
|
})
|
2018-03-02 13:37:36 +00:00
|
|
|
.with_second(0)
|
2018-06-08 00:20:00 +00:00
|
|
|
.unwrap() // safe because second = 0 is safe
|
2017-08-29 14:26:57 +00:00
|
|
|
},
|
2018-02-04 18:50:05 +00:00
|
|
|
|
|
|
|
Timed::Secondly => {
|
|
|
|
let min = create
|
|
|
|
.value_of("minute")
|
2018-11-08 14:52:03 +00:00
|
|
|
.map_dbg(|m| format!("minute = {:?}", m))
|
2018-02-04 18:50:05 +00:00
|
|
|
.and_then(|s| {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
.map_err(|_| warn!("Could not parse minute: '{}'", s))
|
|
|
|
.ok()
|
|
|
|
})
|
2018-06-08 00:19:55 +00:00
|
|
|
.unwrap_or(ndt.minute());
|
2018-02-04 18:50:05 +00:00
|
|
|
|
|
|
|
let sec = create
|
|
|
|
.value_of("second")
|
2018-11-08 14:52:03 +00:00
|
|
|
.map_dbg(|s| format!("second = {:?}", s))
|
2018-02-04 18:50:05 +00:00
|
|
|
.and_then(|s| {
|
|
|
|
FromStr::from_str(s)
|
|
|
|
.map_err(|_| warn!("Could not parse second: '{}'", s))
|
|
|
|
.ok()
|
|
|
|
})
|
2018-06-08 00:19:55 +00:00
|
|
|
.unwrap_or(ndt.second());
|
2018-02-04 18:50:05 +00:00
|
|
|
|
2018-06-08 00:20:00 +00:00
|
|
|
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)
|
|
|
|
})
|
2018-02-04 18:50:05 +00:00
|
|
|
},
|
2017-08-29 14:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|