From 53d12a867004aa4409799f0d71cb15a66b6a28b0 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 23:27:40 +0100 Subject: [PATCH 1/6] Use "config" instead of "yaml-rust" --- Cargo.lock | 15 ++++++++++++++- Cargo.toml | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) 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" From 099d63f13ace1bda54d9f6f0fdc254eb697541d4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 23:51:44 +0100 Subject: [PATCH 2/6] Implement simple configuration module --- src/config.rs | 26 ------------------- src/configuration.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++-- 3 files changed, 62 insertions(+), 28 deletions(-) delete mode 100644 src/config.rs create mode 100644 src/configuration.rs 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..3d15bcc3 --- /dev/null +++ b/src/configuration.rs @@ -0,0 +1,59 @@ +extern crate clap; + +use cli::Config; + +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 verbose : bool, + pub debugging : bool, +} + +impl Configuration { + + pub fn new(config: &Config) -> Configuration { + let rtp = rtp_path(config); + + let mut verbose = false; + let mut debugging = false; + + 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; + } + } + + Configuration { + verbose: verbose, + debugging: debugging, + rtp: rtp, + } + } + + pub fn is_verbose(&self) -> bool { + self.verbose + } + + pub fn is_debugging(&self) -> bool { + self.debugging + } + +} + +fn rtp_path(config: &Config) -> 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..e447b5ac 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 configuration::Configuration; use runtime::{ImagLogger, Runtime}; use clap::App; mod cli; -mod config; +mod configuration; mod runtime; mod module; mod storage; From 754ad0c6cbbbf62cd5e4aab299a3a895ebe9349c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 23:56:54 +0100 Subject: [PATCH 3/6] Add store path configuration --- src/configuration.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/configuration.rs b/src/configuration.rs index 3d15bcc3..17daa91f 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -11,6 +11,7 @@ use config::types::ScalarValue as S; pub struct Configuration { pub rtp : String, + pub store_sub : String, pub verbose : bool, pub debugging : bool, } @@ -22,6 +23,7 @@ impl Configuration { 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") { @@ -30,11 +32,15 @@ impl Configuration { 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, } } @@ -47,6 +53,10 @@ impl Configuration { self.debugging } + pub fn store_path_str(&self) -> String { + format!("{}{}", self.rtp, self.store_sub) + } + } fn rtp_path(config: &Config) -> String { From 497cdde581dc5fde5295c6bb880e8fbe41782deb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 26 Oct 2015 23:59:24 +0100 Subject: [PATCH 4/6] Logger should respect configuration --- src/main.rs | 2 +- src/runtime.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index e447b5ac..c17a3fe0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ fn main() { let mut config = Config::new(app); let configuration = Configuration::new(&config); - let logger = ImagLogger::init(&config); + let logger = ImagLogger::init(&configuration, &config); let rt = Runtime::new(config); info!("Hello, world!"); diff --git a/src/runtime.rs b/src/runtime.rs index def9a8f7..b7f8c40c 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,6 +1,7 @@ extern crate log; pub use cli::Config; +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: &Config) -> 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) From 904d3fa8c0ede3172d7a188f3fde61d1936ba5d1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 27 Oct 2015 00:01:11 +0100 Subject: [PATCH 5/6] Pass configuration from cfg file to Runtime as well --- src/main.rs | 2 +- src/runtime.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c17a3fe0..ffce2d4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn main() { let configuration = Configuration::new(&config); let logger = ImagLogger::init(&configuration, &config); - let rt = Runtime::new(config); + let rt = Runtime::new(configuration, config); info!("Hello, world!"); } diff --git a/src/runtime.rs b/src/runtime.rs index b7f8c40c..f781181a 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -59,22 +59,24 @@ impl log::Log for ImagLogger { pub struct Runtime<'a> { pub config : Config<'a>, + pub configuration : Cfg, } impl<'a> Runtime<'a> { - pub fn new(config : Config<'a>) -> Runtime<'a> { + pub fn new(cfg: Cfg, config : Config<'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() } } From b98dea3946b9ec01adeab6dfee3e65a26f2f8168 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 27 Oct 2015 00:02:42 +0100 Subject: [PATCH 6/6] Rename "Config" to "CliConfig" --- src/cli.rs | 8 ++++---- src/configuration.rs | 6 +++--- src/main.rs | 4 ++-- src/runtime.rs | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) 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/configuration.rs b/src/configuration.rs index 17daa91f..d6d6643b 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -1,6 +1,6 @@ extern crate clap; -use cli::Config; +use cli::CliConfig; use std::path::Path; use clap::{App, ArgMatches}; @@ -18,7 +18,7 @@ pub struct Configuration { impl Configuration { - pub fn new(config: &Config) -> Configuration { + pub fn new(config: &CliConfig) -> Configuration { let rtp = rtp_path(config); let mut verbose = false; @@ -59,7 +59,7 @@ impl Configuration { } -fn rtp_path(config: &Config) -> String { +fn rtp_path(config: &CliConfig) -> String { String::from(config.cli_matches.value_of("rtp").unwrap_or("~/.imag/store/")) } diff --git a/src/main.rs b/src/main.rs index ffce2d4a..704e1c14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ #[macro_use] extern crate log; extern crate config; -use cli::Config; +use cli::CliConfig; use configuration::Configuration; use runtime::{ImagLogger, Runtime}; use clap::App; @@ -17,7 +17,7 @@ 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(&configuration, &config); diff --git a/src/runtime.rs b/src/runtime.rs index f781181a..25e61edc 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -1,6 +1,6 @@ extern crate log; -pub use cli::Config; +pub use cli::CliConfig; pub use configuration::Configuration as Cfg; use std::io::stderr; @@ -23,7 +23,7 @@ impl ImagLogger { ImagLogger::init_logger(LogLevelFilter::Error) } - pub fn init(cfg: &Cfg, config: &Config) -> Result<(), SetLoggerError> { + 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() || cfg.is_debugging() { @@ -58,13 +58,13 @@ 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(cfg: Cfg, config : Config<'a>) -> Runtime<'a> { + pub fn new(cfg: Cfg, config : CliConfig<'a>) -> Runtime<'a> { Runtime { config: config, configuration: cfg,