Merge pull request #571 from matthiasbeyer/libimagrt/config-override-settings

Libimagrt/config override settings
This commit is contained in:
Matthias Beyer 2016-07-24 20:40:45 +02:00 committed by GitHub
commit 8687e0a1a9
2 changed files with 104 additions and 2 deletions

View file

@ -6,7 +6,12 @@ use toml::{Parser, Value};
generate_error_module!(
generate_error_types!(ConfigError, ConfigErrorKind,
NoConfigFileFound => "No config file found"
NoConfigFileFound => "No config file found",
ConfigOverrideError => "Config override error",
ConfigOverrideKeyNotAvailable => "Key not available",
ConfigOverrideTypeNotMatching => "Configuration Type not matching"
);
);
@ -82,6 +87,72 @@ impl Configuration {
}
}
/// Override the configuration.
/// The `v` parameter is expected to contain 'key=value' pairs where the key is a path in the
/// TOML tree, the value to be an appropriate value.
///
/// The override fails if the configuration which is about to be overridden does not exist or
/// the `value` part cannot be converted to the type of the configuration value.
///
/// If `v` is empty, this is considered to be a successful `override_config()` call.
pub fn override_config(&mut self, v: Vec<String>) -> Result<()> {
use libimagutil::key_value_split::*;
use libimagutil::iter::*;
use self::error::ConfigErrorKind as CEK;
use libimagerror::into::IntoError;
v.into_iter()
.map(|s| { debug!("Trying to process '{}'", s); s })
.filter_map(|s| {
match s.into_kv() {
Some(kv) => Some(kv.into()),
None => {
warn!("Could split at '=' - will be ignore override");
None
}
}
})
.map(|(k, v)| {
match self.config.lookup_mut(&k[..]) {
Some(value) => {
match into_value(value, v) {
Some(v) => {
*value = v;
info!("Successfully overridden: {} = {}", k, value);
Ok(())
},
None => Err(CEK::ConfigOverrideTypeNotMatching.into_error()),
}
},
None => Err(CEK::ConfigOverrideKeyNotAvailable.into_error()),
}
})
.fold_defresult(|i| i)
.map_err(Box::new)
.map_err(|e| CEK::ConfigOverrideError.into_error_with_cause(e))
}
}
/// Tries to convert the String `s` into the same type as `value`.
///
/// Returns None if string cannot be converted.
///
/// Arrays and Tables are not supported and will yield `None`.
fn into_value(value: &Value, s: String) -> Option<Value> {
use std::str::FromStr;
match value {
&Value::String(_) => Some(Value::String(s)),
&Value::Integer(_) => FromStr::from_str(&s[..]).ok().map(|i| Value::Integer(i)),
&Value::Float(_) => FromStr::from_str(&s[..]).ok().map(|f| Value::Float(f)),
&Value::Boolean(_) => {
if s == "true" { Some(Value::Boolean(true)) }
else if s == "false" { Some(Value::Boolean(false)) }
else { None }
}
&Value::Datetime(_) => Some(Value::Datetime(s)),
_ => None,
}
}
impl Deref for Configuration {

View file

@ -88,7 +88,16 @@ impl<'a> Runtime<'a> {
None
},
Ok(cfg) => Some(cfg),
Ok(mut cfg) => {
if let Err(e) = cfg.override_config(get_override_specs(&matches)) {
error!("Could not apply config overrides");
trace_error(&e);
// TODO: continue question (interactive)
}
Some(cfg)
}
};
let store_config = match cfg {
@ -179,6 +188,12 @@ impl<'a> Runtime<'a> {
.required(false)
.takes_value(true))
.arg(Arg::with_name("config-override")
.long("override-config")
.help("Override a configuration settings. Use 'key=value' pairs, where the key is a path in the TOML configuration. The value must be present in the configuration and be convertible to the type of the configuration setting. If the argument does not contain a '=', it gets ignored. Setting Arrays and Tables is not yet supported.")
.required(false)
.takes_value(true))
.arg(Arg::with_name("runtimepath")
.long("rtp")
.help("Alternative runtimepath")
@ -285,3 +300,19 @@ impl<'a> Runtime<'a> {
}
}
fn get_override_specs(matches: &ArgMatches) -> Vec<String> {
matches
.values_of("config-override")
.map(|values| {
values
.filter(|s| {
let b = s.contains("=");
if !b { warn!("override '{}' does not contain '=' - will be ignored!", s); }
b
})
.map(String::from)
.collect()
})
.unwrap_or(vec![])
}