diff --git a/Cargo.lock b/Cargo.lock index 0cc7c2ee..fcf992e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,13 +4,13 @@ version = "0.1.0" dependencies = [ "chrono 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", "clap 1.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "config 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "rustty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.37 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", - "yaml-rust 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -60,6 +60,14 @@ dependencies = [ "yaml-rust 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "config" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "kernel32-sys" version = "0.1.4" @@ -109,6 +117,11 @@ dependencies = [ "libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nom" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "num" version = "0.1.27" diff --git a/Cargo.toml b/Cargo.toml index 5574f82b..3a6d3185 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ regex = "0.1.41" url = "0.2.37" uuid = "0.1.18" -yaml-rust = "0.2.2" +config = "0.1.2" chrono = "0.2.16" diff --git a/src/cli.rs b/src/cli.rs index bf0528a3..7d148456 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -13,14 +13,14 @@ impl ModuleConfig { } } -pub struct Config<'a> { +pub struct CliConfig<'a> { pub module_configs : Vec, pub cli_matches : ArgMatches<'a, 'a>, } -impl<'a> Config<'a> { - pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> Config<'a> { - Config { +impl<'a> CliConfig<'a> { + pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> CliConfig<'a> { + CliConfig { module_configs: vec![], cli_matches: app.get_matches(), } diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index 9ceea9b9..00000000 --- a/src/config.rs +++ /dev/null @@ -1,26 +0,0 @@ -extern crate clap; - -use cli::Config; - -use std::path::Path; -use clap::{App, ArgMatches}; - -pub struct Configuration { - pub rtp : String, -} - -impl Configuration { - - pub fn new(config: &Config) -> Configuration { - Configuration { - rtp: rtp_path(config), - } - } - -} - -fn rtp_path(config: &Config) -> String { - String::from(config.cli_matches.value_of("rtp").unwrap_or("~/.imag/store/")) -} - - diff --git a/src/configuration.rs b/src/configuration.rs new file mode 100644 index 00000000..d6d6643b --- /dev/null +++ b/src/configuration.rs @@ -0,0 +1,69 @@ +extern crate clap; + +use cli::CliConfig; + +use std::path::Path; +use clap::{App, ArgMatches}; +use config::reader::from_file; +use config::types::Config as Cfg; +use config::types::Value as V; +use config::types::ScalarValue as S; + +pub struct Configuration { + pub rtp : String, + pub store_sub : String, + pub verbose : bool, + pub debugging : bool, +} + +impl Configuration { + + pub fn new(config: &CliConfig) -> Configuration { + let rtp = rtp_path(config); + + 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(v) = cfg.lookup_boolean("verbose") { + verbose = v; + } + if let Some(d) = cfg.lookup_boolean("debug") { + debugging = d; + } + if let Some(s) = cfg.lookup_str("store") { + store_sub = String::from(s); + } + } + + Configuration { + verbose: verbose, + debugging: debugging, + store_sub: store_sub, + rtp: rtp, + } + } + + pub fn is_verbose(&self) -> bool { + self.verbose + } + + pub fn is_debugging(&self) -> bool { + self.debugging + } + + pub fn store_path_str(&self) -> String { + format!("{}{}", self.rtp, self.store_sub) + } + +} + +fn rtp_path(config: &CliConfig) -> String { + String::from(config.cli_matches.value_of("rtp").unwrap_or("~/.imag/store/")) +} + +fn fetch_config(rtp: &String) -> Option { + from_file(Path::new(&(rtp.clone() + "/config"))).ok() +} + diff --git a/src/main.rs b/src/main.rs index 2c34de97..704e1c14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ #[macro_use] extern crate clap; #[macro_use] extern crate log; +extern crate config; -use cli::Config; -use config::Configuration; +use cli::CliConfig; +use configuration::Configuration; use runtime::{ImagLogger, Runtime}; use clap::App; mod cli; -mod config; +mod configuration; mod runtime; mod module; mod storage; @@ -16,11 +17,11 @@ fn main() { let early_logger = ImagLogger::early().unwrap(); let yaml = load_yaml!("../etc/cli.yml"); let app = App::from_yaml(yaml); - let mut config = Config::new(app); + let mut config = CliConfig::new(app); let configuration = Configuration::new(&config); - let logger = ImagLogger::init(&config); - let rt = Runtime::new(config); + let logger = ImagLogger::init(&configuration, &config); + let rt = Runtime::new(configuration, config); info!("Hello, world!"); } diff --git a/src/runtime.rs b/src/runtime.rs index def9a8f7..25e61edc 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,6 +1,7 @@ extern crate log; -pub use cli::Config; +pub use cli::CliConfig; +pub use configuration::Configuration as Cfg; use std::io::stderr; use std::io::Write; @@ -22,10 +23,10 @@ impl ImagLogger { ImagLogger::init_logger(LogLevelFilter::Error) } - pub fn init(config: &Config) -> Result<(), SetLoggerError> { - if config.is_debugging() { + pub fn init(cfg: &Cfg, config: &CliConfig) -> Result<(), SetLoggerError> { + if config.is_debugging() || cfg.is_debugging() { ImagLogger::init_logger(LogLevelFilter::Debug) - } else if config.is_verbose() { + } else if config.is_verbose() || cfg.is_debugging() { ImagLogger::init_logger(LogLevelFilter::Info) } else { ImagLogger::init_logger(LogLevelFilter::Error) @@ -57,23 +58,25 @@ impl log::Log for ImagLogger { } pub struct Runtime<'a> { - pub config : Config<'a>, + pub config : CliConfig<'a>, + pub configuration : Cfg, } impl<'a> Runtime<'a> { - pub fn new(config : Config<'a>) -> Runtime<'a> { + pub fn new(cfg: Cfg, config : CliConfig<'a>) -> Runtime<'a> { Runtime { config: config, + configuration: cfg, } } pub fn is_verbose(&self) -> bool { - self.config.is_verbose() + self.config.is_verbose() || self.configuration.is_verbose() } pub fn is_debugging(&self) -> bool { - self.config.is_debugging() + self.config.is_debugging() || self.configuration.is_verbose() } }