Merge pull request #1064 from matthiasbeyer/imag-diary/per-diary-timed-config
imag-diary: per diary timed config
This commit is contained in:
commit
05c7467866
6 changed files with 103 additions and 20 deletions
|
@ -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" }
|
||||
|
|
|
@ -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") {
|
||||
|
||||
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)
|
||||
} else {
|
||||
let id = create_id_from_clispec(&create, &diaryname);
|
||||
diary.retrieve(id).chain_err(|| DEK::StoreReadError)
|
||||
}
|
||||
};
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -76,3 +76,9 @@ readline_prompt = ">> "
|
|||
# lives implicitely
|
||||
implicit-create = false
|
||||
|
||||
[diary]
|
||||
default_diary = "default"
|
||||
|
||||
[diary.diaries.default]
|
||||
timed = "minutely"
|
||||
|
||||
|
|
Loading…
Reference in a new issue