Merge branch 'cleanup-runtime'

This commit is contained in:
Matthias Beyer 2015-12-01 18:40:02 +01:00
commit 2e3db902e0
4 changed files with 48 additions and 9 deletions

View file

@ -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

View file

@ -41,6 +41,14 @@ impl<'a> CliConfig<'a> {
pub fn get_rtp(&self) -> Option<String> {
self.cli_matches.value_of("rtp").and_then(|s| Some(String::from(s)))
}
pub fn store_path(&self) -> Option<String> {
self.get_rtp().and_then(|rtp| {
self.cli_matches
.value_of("storepath")
.and_then(|s| Some(rtp + s))
})
}
}
impl<'a> Debug for CliConfig<'a> {

View file

@ -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<String> {
config.cli_matches.value_of("rtp")
.and_then(|s| Some(String::from(s)))
}
fn fetch_config(rtp: &String) -> Option<Cfg> {
from_file(Path::new(&(rtp.clone() + "/config"))).ok()
fn fetch_config(rtp: Option<String>) -> Option<Cfg> {
rtp.and_then(|r| from_file(Path::new(&(r.clone() + "/config"))).ok())
}
fn default_path() -> Option<String> {
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()
)
}

View file

@ -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