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> { diff --git a/src/configuration.rs b/src/configuration.rs index a4ff2971..c8eb8e67 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; } @@ -38,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, + rtp: runtimepath, } } @@ -54,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) } @@ -64,12 +74,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 { @@ -79,7 +100,7 @@ impl Debug for Configuration { self.is_verbose(), self.is_debugging(), self.get_rtp(), - self.store_path_str() + self.store_path() ) } 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