Merge branch 'config-file'

This commit is contained in:
Matthias Beyer 2015-10-27 00:02:55 +01:00
commit a203b7af95
7 changed files with 106 additions and 46 deletions

15
Cargo.lock generated
View file

@ -4,13 +4,13 @@ version = "0.1.0"
dependencies = [ dependencies = [
"chrono 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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)", "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)", "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]] [[package]]
@ -60,6 +60,14 @@ dependencies = [
"yaml-rust 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "kernel32-sys" name = "kernel32-sys"
version = "0.1.4" version = "0.1.4"
@ -109,6 +117,11 @@ dependencies = [
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", "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]] [[package]]
name = "num" name = "num"
version = "0.1.27" version = "0.1.27"

View file

@ -9,7 +9,7 @@ regex = "0.1.41"
url = "0.2.37" url = "0.2.37"
uuid = "0.1.18" uuid = "0.1.18"
yaml-rust = "0.2.2" config = "0.1.2"
chrono = "0.2.16" chrono = "0.2.16"

View file

@ -13,14 +13,14 @@ impl ModuleConfig {
} }
} }
pub struct Config<'a> { pub struct CliConfig<'a> {
pub module_configs : Vec<ModuleConfig>, pub module_configs : Vec<ModuleConfig>,
pub cli_matches : ArgMatches<'a, 'a>, pub cli_matches : ArgMatches<'a, 'a>,
} }
impl<'a> Config<'a> { impl<'a> CliConfig<'a> {
pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> Config<'a> { pub fn new(app : clap::App<'a, 'a, 'a, 'a, 'a, 'a>) -> CliConfig<'a> {
Config { CliConfig {
module_configs: vec![], module_configs: vec![],
cli_matches: app.get_matches(), cli_matches: app.get_matches(),
} }

View file

@ -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/"))
}

69
src/configuration.rs Normal file
View file

@ -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<Cfg> {
from_file(Path::new(&(rtp.clone() + "/config"))).ok()
}

View file

@ -1,13 +1,14 @@
#[macro_use] extern crate clap; #[macro_use] extern crate clap;
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate config;
use cli::Config; use cli::CliConfig;
use config::Configuration; use configuration::Configuration;
use runtime::{ImagLogger, Runtime}; use runtime::{ImagLogger, Runtime};
use clap::App; use clap::App;
mod cli; mod cli;
mod config; mod configuration;
mod runtime; mod runtime;
mod module; mod module;
mod storage; mod storage;
@ -16,11 +17,11 @@ fn main() {
let early_logger = ImagLogger::early().unwrap(); let early_logger = ImagLogger::early().unwrap();
let yaml = load_yaml!("../etc/cli.yml"); let yaml = load_yaml!("../etc/cli.yml");
let app = App::from_yaml(yaml); let app = App::from_yaml(yaml);
let mut config = Config::new(app); let mut config = CliConfig::new(app);
let configuration = Configuration::new(&config); let configuration = Configuration::new(&config);
let logger = ImagLogger::init(&config); let logger = ImagLogger::init(&configuration, &config);
let rt = Runtime::new(config); let rt = Runtime::new(configuration, config);
info!("Hello, world!"); info!("Hello, world!");
} }

View file

@ -1,6 +1,7 @@
extern crate log; extern crate log;
pub use cli::Config; pub use cli::CliConfig;
pub use configuration::Configuration as Cfg;
use std::io::stderr; use std::io::stderr;
use std::io::Write; use std::io::Write;
@ -22,10 +23,10 @@ impl ImagLogger {
ImagLogger::init_logger(LogLevelFilter::Error) ImagLogger::init_logger(LogLevelFilter::Error)
} }
pub fn init(config: &Config) -> Result<(), SetLoggerError> { pub fn init(cfg: &Cfg, config: &CliConfig) -> Result<(), SetLoggerError> {
if config.is_debugging() { if config.is_debugging() || cfg.is_debugging() {
ImagLogger::init_logger(LogLevelFilter::Debug) ImagLogger::init_logger(LogLevelFilter::Debug)
} else if config.is_verbose() { } else if config.is_verbose() || cfg.is_debugging() {
ImagLogger::init_logger(LogLevelFilter::Info) ImagLogger::init_logger(LogLevelFilter::Info)
} else { } else {
ImagLogger::init_logger(LogLevelFilter::Error) ImagLogger::init_logger(LogLevelFilter::Error)
@ -57,23 +58,25 @@ impl log::Log for ImagLogger {
} }
pub struct Runtime<'a> { pub struct Runtime<'a> {
pub config : Config<'a>, pub config : CliConfig<'a>,
pub configuration : Cfg,
} }
impl<'a> Runtime<'a> { impl<'a> Runtime<'a> {
pub fn new(config : Config<'a>) -> Runtime<'a> { pub fn new(cfg: Cfg, config : CliConfig<'a>) -> Runtime<'a> {
Runtime { Runtime {
config: config, config: config,
configuration: cfg,
} }
} }
pub fn is_verbose(&self) -> bool { pub fn is_verbose(&self) -> bool {
self.config.is_verbose() self.config.is_verbose() || self.configuration.is_verbose()
} }
pub fn is_debugging(&self) -> bool { pub fn is_debugging(&self) -> bool {
self.config.is_debugging() self.config.is_debugging() || self.configuration.is_verbose()
} }
} }