From 56cd6dc4ae0a0620f456242e6350dbea69af851a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 1 Dec 2015 18:19:57 +0100 Subject: [PATCH 1/5] Configuration::new() should use default values if the configuration can not be loaded. --- src/configuration.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index a4ff2971..bee3e47f 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -20,13 +20,15 @@ pub struct Configuration { impl Configuration { pub fn new(config: &CliConfig) -> Configuration { - let rtp = rtp_path(config); + use std::env::home_dir; + + let rtp = rtp_path(config).or(default_path()); let mut verbose = false; let mut debugging = false; let mut store_sub = String::from("/store"); - if let Some(cfg) = fetch_config(&rtp) { + if let Some(cfg) = fetch_config(rtp.clone()) { if let Some(v) = cfg.lookup_boolean("verbose") { verbose = v; } @@ -42,7 +44,7 @@ impl Configuration { verbose: verbose, debugging: debugging, store_sub: store_sub, - rtp: rtp, + rtp: rtp.unwrap_or(String::from("/tmp/")), } } @@ -64,12 +66,23 @@ impl Configuration { } -fn rtp_path(config: &CliConfig) -> String { - String::from(config.cli_matches.value_of("rtp").unwrap_or("~/.imag/store/")) +fn rtp_path(config: &CliConfig) -> Option { + config.cli_matches.value_of("rtp") + .and_then(|s| Some(String::from(s))) } -fn fetch_config(rtp: &String) -> Option { - from_file(Path::new(&(rtp.clone() + "/config"))).ok() +fn fetch_config(rtp: Option) -> Option { + rtp.and_then(|r| from_file(Path::new(&(r.clone() + "/config"))).ok()) +} + +fn default_path() -> Option { + use std::env::home_dir; + + home_dir().and_then(|mut buf| { + buf.push("/.imag"); + buf.to_str().map(|s| String::from(s)) + }) + } impl Debug for Configuration { From 5f4718a2e596641f30961a049fc045b7b664692f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 1 Dec 2015 18:32:00 +0100 Subject: [PATCH 2/5] Add debug output for Configuration::new() --- src/configuration.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/configuration.rs b/src/configuration.rs index bee3e47f..385df338 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -40,11 +40,19 @@ impl Configuration { } } + let runtimepath = rtp.unwrap_or(String::from("/tmp/")); + + debug!("Building configuration"); + debug!(" - verbose : {}", verbose); + debug!(" - debugging : {}", debugging); + debug!(" - store sub : {}", store_sub); + debug!(" - runtimepath: {}", runtimepath); + Configuration { verbose: verbose, debugging: debugging, store_sub: store_sub, - rtp: rtp.unwrap_or(String::from("/tmp/")), + rtp: runtimepath, } } From da85c3ffe7ab24aa371d130b9a3f2798fb6e849a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 1 Dec 2015 18:36:04 +0100 Subject: [PATCH 3/5] Rename function: store_path_str() -> store_path() --- src/configuration.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index 385df338..c8eb8e67 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -64,7 +64,7 @@ impl Configuration { self.debugging } - pub fn store_path_str(&self) -> String { + pub fn store_path(&self) -> String { format!("{}{}", self.rtp, self.store_sub) } @@ -100,7 +100,7 @@ impl Debug for Configuration { self.is_verbose(), self.is_debugging(), self.get_rtp(), - self.store_path_str() + self.store_path() ) } From 5942877c43a96af939b6b66d941a7567269aab93 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 1 Dec 2015 18:37:29 +0100 Subject: [PATCH 4/5] Add possibility to override store name in CLI --- etc/cli.yml | 6 ++++++ src/cli.rs | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/etc/cli.yml b/etc/cli.yml index 93eed6cb..d86660ea 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -21,6 +21,12 @@ args: help: Set the runtime path required: false + - storename: + short: s + long: storename + help: Name of the store in the runtimepath. Defaults to "store" + required: false + subcommands: - cal: about: Calendar module diff --git a/src/cli.rs b/src/cli.rs index 2a4f25bc..42b8d563 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -41,6 +41,14 @@ impl<'a> CliConfig<'a> { pub fn get_rtp(&self) -> Option { self.cli_matches.value_of("rtp").and_then(|s| Some(String::from(s))) } + + pub fn store_path(&self) -> Option { + self.get_rtp().and_then(|rtp| { + self.cli_matches + .value_of("storepath") + .and_then(|s| Some(rtp + s)) + }) + } } impl<'a> Debug for CliConfig<'a> { From a5dabe55a9a397c6351df7bfea59bef0902e4daf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 1 Dec 2015 18:37:44 +0100 Subject: [PATCH 5/5] Provide getter for store path in Runtime object --- src/runtime.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/runtime.rs b/src/runtime.rs index 5901b439..a666c1bf 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -74,6 +74,10 @@ impl<'a> Runtime<'a> { self.config.is_debugging() || self.configuration.is_verbose() } + pub fn store_path(&self) -> String { + self.config.store_path().unwrap_or(self.configuration.store_path()) + } + pub fn get_rtp(&self) -> String { if let Some(rtp) = self.config.get_rtp() { rtp