From e4036c531b8411810215bcc4b74e943a38bcf4ec Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 14 Sep 2017 18:57:59 +0200 Subject: [PATCH 1/5] Add example setting for diary --- imagrc.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/imagrc.toml b/imagrc.toml index 0c6a90a7..5fd90653 100644 --- a/imagrc.toml +++ b/imagrc.toml @@ -76,3 +76,9 @@ readline_prompt = ">> " # lives implicitely implicit-create = false +[diary] +default_diary = "default" + +[diary.diaries.default] +timed = "minutely" + From ea982204b9e527610570d494202358cf32c73e3d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 14 Sep 2017 18:58:17 +0200 Subject: [PATCH 2/5] Add new deps: toml, toml_query --- bin/domain/imag-diary/Cargo.toml | 2 ++ bin/domain/imag-diary/src/main.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/bin/domain/imag-diary/Cargo.toml b/bin/domain/imag-diary/Cargo.toml index cc7e956a..0f1ad734 100644 --- a/bin/domain/imag-diary/Cargo.toml +++ b/bin/domain/imag-diary/Cargo.toml @@ -18,6 +18,8 @@ chrono = "0.4" version = "2.0" clap = "2.*" log = "0.3" +toml = "0.4" +toml-query = "0.3.1" libimagerror = { version = "0.4.0", path = "../../../lib/core/libimagerror" } libimagstore = { version = "0.4.0", path = "../../../lib/core/libimagstore" } diff --git a/bin/domain/imag-diary/src/main.rs b/bin/domain/imag-diary/src/main.rs index 844ef3e4..4502185f 100644 --- a/bin/domain/imag-diary/src/main.rs +++ b/bin/domain/imag-diary/src/main.rs @@ -36,6 +36,8 @@ #[macro_use] extern crate version; extern crate clap; extern crate chrono; +extern crate toml; +extern crate toml_query; extern crate libimagdiary; extern crate libimagentryedit; From dbf529e427865421a37f67f07508d244e7bf8928 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 14 Sep 2017 18:58:40 +0200 Subject: [PATCH 3/5] Add config parsing helpers --- bin/domain/imag-diary/src/util.rs | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/bin/domain/imag-diary/src/util.rs b/bin/domain/imag-diary/src/util.rs index 25498085..a6ed9342 100644 --- a/bin/domain/imag-diary/src/util.rs +++ b/bin/domain/imag-diary/src/util.rs @@ -18,6 +18,10 @@ // use libimagrt::runtime::Runtime; +use libimagdiary::error::*; + +use toml::Value; +use toml_query::read::TomlValueReadExt; pub fn get_diary_name(rt: &Runtime) -> Option { use libimagdiary::config::get_default_diary_name; @@ -26,3 +30,56 @@ pub fn get_diary_name(rt: &Runtime) -> Option { .or(rt.cli().value_of("diaryname").map(String::from)) } +pub enum Timed { + Hourly, + Minutely, +} + +/// Returns true if the diary should always create timed entries, which is whenever +/// +/// ```toml +/// diary.diaries..timed = true +/// ``` +/// +/// # Returns +/// +/// * Ok(Some(Timed::Hourly)) if diary should create timed entries +/// * Ok(Some(Timed::Minutely)) if diary should not create timed entries +/// * Ok(None) if config is not available +/// * Err(e) if reading the toml failed, type error or something like this +/// +pub fn get_diary_timed_config(rt: &Runtime, diary_name: &str) -> Result> { + match rt.config() { + None => Ok(None), + Some(cfg) => { + let v = cfg + .config() + .read(&format!("diary.diaries.{}.timed", diary_name)) + .chain_err(|| DiaryErrorKind::IOError); + + match v { + Ok(Some(&Value::String(ref s))) => parse_timed_string(s, diary_name).map(Some), + + Ok(Some(_)) => { + let s = format!("Type error at 'diary.diaryies.{}.timed': should be either 'h'/'hourly' or 'm'/'minutely'", diary_name); + Err(s).map_err(From::from) + }, + + Ok(None) => Ok(None), + Err(e) => Err(e), + } + } + } +} + +pub fn parse_timed_string(s: &str, diary_name: &str) -> Result { + if s == "h" || s == "hourly" { + Ok(Timed::Hourly) + } else if s == "m" || s == "minutely" { + Ok(Timed::Minutely) + } else { + let s = format!("Cannot parse config: 'diary.diaries.{}.timed = {}'", + diary_name, s); + Err(s).map_err(From::from) + } +} From dc78c0e448d3e8726b62e950a1730911d878f4e1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 14 Sep 2017 18:59:03 +0200 Subject: [PATCH 4/5] Change create() to check config and commandline whether entry should be created timed or not --- bin/domain/imag-diary/src/create.rs | 53 ++++++++++++++++++----------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/bin/domain/imag-diary/src/create.rs b/bin/domain/imag-diary/src/create.rs index 69760399..fd4b73bc 100644 --- a/bin/domain/imag-diary/src/create.rs +++ b/bin/domain/imag-diary/src/create.rs @@ -17,8 +17,6 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use std::process::exit; - use clap::ArgMatches; use libimagdiary::diary::Diary; @@ -28,11 +26,14 @@ use libimagdiary::error::ResultExt; use libimagentryedit::edit::Edit; use libimagrt::runtime::Runtime; use libimagerror::trace::trace_error_exit; +use libimagerror::trace::MapErrTrace; use libimagutil::warn_exit::warn_exit; use libimagstore::store::FileLockEntry; use libimagstore::store::Store; use util::get_diary_name; +use util::get_diary_timed_config; +use util::Timed; pub fn create(rt: &Runtime) { let diaryname = get_diary_name(rt) @@ -57,13 +58,33 @@ pub fn create(rt: &Runtime) { } fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLockEntry<'a> { + use util::parse_timed_string; + let create = rt.cli().subcommand_matches("create").unwrap(); - let entry = if !create.is_present("timed") { - debug!("Creating non-timed entry"); - diary.new_entry_today(diaryname) - } else { - let id = create_id_from_clispec(&create, &diaryname); - diary.retrieve(id).chain_err(|| DEK::StoreReadError) + + let create_timed = create.value_of("timed") + .map(|t| parse_timed_string(t, diaryname).map_err_trace_exit(1).unwrap()) + .map(Some) + .unwrap_or_else(|| match get_diary_timed_config(rt, diaryname) { + Err(e) => trace_error_exit(&e, 1), + Ok(Some(t)) => Some(t), + Ok(None) => { + warn!("Missing config: 'diary.diaries.{}.timed'", diaryname); + warn!("Assuming 'false'"); + None + } + }); + + let entry = match create_timed { + Some(timed) => { + let id = create_id_from_clispec(&create, &diaryname, timed); + diary.retrieve(id).chain_err(|| DEK::StoreReadError) + }, + + None => { + debug!("Creating non-timed entry"); + diary.new_entry_today(diaryname) + } }; match entry { @@ -76,7 +97,7 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock } -fn create_id_from_clispec(create: &ArgMatches, diaryname: &str) -> DiaryId { +fn create_id_from_clispec(create: &ArgMatches, diaryname: &str, timed_type: Timed) -> DiaryId { use std::str::FromStr; let get_hourly_id = |create: &ArgMatches| -> DiaryId { @@ -94,13 +115,13 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str) -> DiaryId { time.with_hour(hr) }; - match create.value_of("timed") { - Some("h") | Some("hourly") => { + match timed_type { + Timed::Hourly => { debug!("Creating hourly-timed entry"); get_hourly_id(create) }, - Some("m") | Some("minutely") => { + Timed::Minutely => { let time = get_hourly_id(create); let min = create .value_of("minute") @@ -114,14 +135,6 @@ fn create_id_from_clispec(create: &ArgMatches, diaryname: &str) -> DiaryId { time.with_minute(min) }, - - Some(_) => { - warn!("Timed creation failed: Unknown spec '{}'", - create.value_of("timed").unwrap()); - exit(1); - }, - - None => warn_exit("Unexpected error, cannot continue", 1) } } From bcb6c7b52d4f0822dd5db83c7394282101c07eaa Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 14 Sep 2017 20:06:20 +0200 Subject: [PATCH 5/5] Add changelog entry --- doc/src/09020-changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md index 9a66de08..9234ba25 100644 --- a/doc/src/09020-changelog.md +++ b/doc/src/09020-changelog.md @@ -29,6 +29,9 @@ This section contains the changelog from the last release to the next release. * The error handling of the whole codebase is based on the `error_chain` now. `libimagerror` only contains convenience functionality, no error-generating macros or such things anymore. + * `imag-diary` can now use a configuration in the imagrc.toml file where for + each diary there is a config whether entries should be created minutely or + hourly (or daily, which is when specifying nothing). * New * `libimagentrygps` was introduced * Fixed bugs