2016-01-28 18:40:58 +00:00
|
|
|
use std::collections::BTreeMap;
|
2016-01-29 15:50:52 +00:00
|
|
|
use std::path::PathBuf;
|
2016-01-28 18:40:58 +00:00
|
|
|
use std::str::Split;
|
|
|
|
|
|
|
|
use clap::ArgMatches;
|
2016-02-01 18:26:41 +00:00
|
|
|
use semver::Version;
|
2016-01-28 18:40:58 +00:00
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
use libimagstore::store::EntryHeader;
|
2016-01-29 15:50:52 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-01-28 18:40:58 +00:00
|
|
|
use libimagutil::key_value_split::IntoKeyValue;
|
|
|
|
|
2016-01-29 15:50:52 +00:00
|
|
|
pub fn build_entry_path(rt: &Runtime, path_elem: &str) -> PathBuf {
|
2016-02-01 18:26:41 +00:00
|
|
|
debug!("Checking path element for version");
|
|
|
|
{
|
|
|
|
let contains_version = {
|
|
|
|
path_elem.split("~")
|
|
|
|
.last()
|
|
|
|
.map(|version| Version::parse(version).is_ok())
|
|
|
|
.unwrap_or(false)
|
|
|
|
};
|
|
|
|
|
|
|
|
if !contains_version {
|
|
|
|
debug!("Version cannot be parsed inside {:?}", path_elem);
|
|
|
|
warn!("Path does not contain version. Will panic now!");
|
|
|
|
panic!("No version in path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("Version checking succeeded");
|
|
|
|
|
|
|
|
debug!("Building path from {:?}", path_elem);
|
2016-01-29 15:50:52 +00:00
|
|
|
let mut path = rt.store().path().clone();
|
|
|
|
|
|
|
|
if path_elem.chars().next() == Some('/') {
|
|
|
|
path.push(&path_elem[1..path_elem.len()]);
|
|
|
|
} else {
|
|
|
|
path.push(path_elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2016-01-29 19:13:44 +00:00
|
|
|
pub fn build_toml_header(matches: &ArgMatches, mut header: EntryHeader) -> EntryHeader {
|
2016-01-28 18:40:58 +00:00
|
|
|
debug!("Building header from cli spec");
|
|
|
|
if let Some(headerspecs) = matches.values_of("header") {
|
2016-01-29 19:13:44 +00:00
|
|
|
let mut main = header.toml_mut();
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("headerspec = {:?}", headerspecs);
|
|
|
|
let kvs = headerspecs.into_iter()
|
|
|
|
.filter_map(|hs| {
|
|
|
|
debug!("- Processing: '{}'", hs);
|
|
|
|
let kv = String::from(hs).into_kv();
|
|
|
|
debug!("- got: '{:?}'", kv);
|
|
|
|
kv
|
|
|
|
});
|
|
|
|
for tpl in kvs {
|
2016-01-28 18:40:58 +00:00
|
|
|
let (key, value) = tpl.into();
|
|
|
|
debug!("Splitting: {:?}", key);
|
|
|
|
let mut split = key.split(".");
|
|
|
|
let current = split.next();
|
|
|
|
if current.is_some() {
|
|
|
|
insert_key_into(String::from(current.unwrap()), &mut split, value, &mut main);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Header = {:?}", header);
|
2016-01-28 18:40:58 +00:00
|
|
|
header
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_key_into(current: String,
|
|
|
|
rest_path: &mut Split<&str>,
|
|
|
|
value: String,
|
|
|
|
map: &mut BTreeMap<String, Value>) {
|
|
|
|
let next = rest_path.next();
|
|
|
|
|
|
|
|
if next.is_none() {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Inserting into {:?} = {:?}", current, value);
|
2016-01-28 18:40:58 +00:00
|
|
|
map.insert(current, parse_value(value));
|
|
|
|
} else {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Inserting into {:?} ... = {:?}", current, value);
|
2016-01-28 18:40:58 +00:00
|
|
|
if map.contains_key(¤t) {
|
|
|
|
match map.get_mut(¤t).unwrap() {
|
|
|
|
&mut Value::Table(ref mut t) => {
|
|
|
|
insert_key_into(String::from(next.unwrap()), rest_path, value, t);
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut submap = BTreeMap::new();
|
|
|
|
insert_key_into(String::from(next.unwrap()), rest_path, value, &mut submap);
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Inserting submap = {:?}", submap);
|
2016-01-28 18:40:58 +00:00
|
|
|
map.insert(current, Value::Table(submap));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_value(value: String) -> Value {
|
2016-01-31 11:54:59 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2016-01-28 18:40:58 +00:00
|
|
|
fn is_ary(v: &String) -> bool {
|
|
|
|
v.chars().next() == Some('[') && v.chars().last() == Some(']') && v.len() >= 3
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "true" {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Building Boolean out of: {:?}...", value);
|
2016-01-28 18:40:58 +00:00
|
|
|
Value::Boolean(true)
|
|
|
|
} else if value == "false" {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Building Boolean out of: {:?}...", value);
|
2016-01-28 18:40:58 +00:00
|
|
|
Value::Boolean(false)
|
|
|
|
} else if is_ary(&value) {
|
2016-01-29 19:14:27 +00:00
|
|
|
debug!("Building Array out of: {:?}...", value);
|
2016-01-28 18:40:58 +00:00
|
|
|
let sub = &value[1..(value.len()-1)];
|
|
|
|
Value::Array(sub.split(",").map(|v| parse_value(String::from(v))).collect())
|
|
|
|
} else {
|
2016-01-31 11:54:59 +00:00
|
|
|
FromStr::from_str(&value[..])
|
|
|
|
.map(|i: i64| {
|
|
|
|
debug!("Building Integer out of: {:?}...", value);
|
|
|
|
Value::Integer(i)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
FromStr::from_str(&value[..])
|
|
|
|
.map(|f: f64| {
|
|
|
|
debug!("Building Float out of: {:?}...", value);
|
|
|
|
Value::Float(f)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|_| {
|
|
|
|
debug!("Building String out of: {:?}...", value);
|
|
|
|
Value::String(value)
|
|
|
|
})
|
|
|
|
})
|
2016-01-28 18:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|