summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-14 20:56:18 +0200
committerGitHub <noreply@github.com>2017-09-14 20:56:18 +0200
commit05c7467866b680865ca7549cac1ed70e3c0a1849 (patch)
treed9f7c2ebb8d547ef0726f6f25430eeee36f88c7a
parent53c7d602eb605593aab03e57230c56e8ff85bd10 (diff)
parentbcb6c7b52d4f0822dd5db83c7394282101c07eaa (diff)
downloadimag-05c7467866b680865ca7549cac1ed70e3c0a1849.zip
imag-05c7467866b680865ca7549cac1ed70e3c0a1849.tar.gz
Merge pull request #1064 from matthiasbeyer/imag-diary/per-diary-timed-config
imag-diary: per diary timed config
-rw-r--r--bin/domain/imag-diary/Cargo.toml2
-rw-r--r--bin/domain/imag-diary/src/create.rs53
-rw-r--r--bin/domain/imag-diary/src/main.rs2
-rw-r--r--bin/domain/imag-diary/src/util.rs57
-rw-r--r--doc/src/09020-changelog.md3
-rw-r--r--imagrc.toml6
6 files changed, 103 insertions, 20 deletions
diff --git a/bin/domain/imag-diary/Cargo.toml b/bin/domain/imag-diary/Cargo.toml
index cc7e956..0f1ad73 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/create.rs b/bin/domain/imag-diary/src/create.rs
index 6976039..fd4b73b 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)
}
}
diff --git a/bin/domain/imag-diary/src/main.rs b/bin/domain/imag-diary/src/main.rs
index 844ef3e..4502185 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;
diff --git a/bin/domain/imag-diary/src/util.rs b/bin/domain/imag-diary/src/util.rs
index 2549808..a6ed934 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<String> {
use libimagdiary::config::get_default_diary_name;
@@ -26,3 +30,56 @@ pub fn get_diary_name(rt: &Runtime) -> Option<String> {
.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.<diary>.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<Option<Timed>> {
+ 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<Timed> {
+ 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)
+ }
+}
diff --git a/doc/src/09020-changelog.md b/doc/src/09020-changelog.md
index 9a66de0..9234ba2 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
diff --git a/imagrc.toml b/imagrc.toml
index 0c6a90a..5fd9065 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"
+