Skip to content

Commit

Permalink
Partial implementation of metadata reading
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Sep 23, 2015
1 parent a7c3917 commit 85df0b4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
104 changes: 98 additions & 6 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;
use std::path::PathBuf;
use std::io::Read;
use std::fs::{File, read_dir};
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::collections::BTreeMap;

Expand All @@ -17,6 +19,14 @@ mod version;
quick_error! {
#[derive(Debug)]
pub enum MetadataError {
DirRead(err: io::Error, path: PathBuf) {
cause(err)
display("error reading dir {:?}: {}", path, err)
description("error reading configuration directory")
}
FileNameDecode(path: PathBuf) {
display("error decoding filename {:?}", path)
}
FileRead(err: io::Error, path: PathBuf) {
cause(err)
display("error reading {:?}: {}", path, err)
Expand All @@ -37,20 +47,24 @@ quick_error! {
}


#[derive(Debug)]
pub struct MetadataErrors {
pub errors: Vec<MetadataError>,
pub partial: Json,
}

#[derive(Debug)]
pub struct TemplateErrors {
pub errors: Vec<TemplateError>,
}

#[derive(Debug)]
pub struct Config {
pub machine: Result<Json, MetadataErrors>,
pub roles: HashMap<String, Role>,
}

#[derive(Debug)]
pub struct Role {
// note version in template is not the same as
pub renderers: HashMap<Version, Result<RenderSet, TemplateErrors>>,
Expand All @@ -71,12 +85,90 @@ impl Cache {
}
}

pub fn read_configs(options: &Options, cache: &mut Cache) -> Config {
let mut cfg = Config {
machine: Ok(Json::Object(BTreeMap::new())),
roles: HashMap::new(),
fn read_meta_entry(path: &Path, ext: &str)
-> Result<Option<Json>, MetadataError>
{
use self::MetadataError::FileRead;
let value = match ext {
"yaml" | "yml" => {
unimplemented!();
}
"json" => {
unimplemented!();
}
"txt" => {
let mut buf = String::with_capacity(100);
try!(File::open(path)
.and_then(|mut f| f.read_to_string(&mut buf))
.map_err(|e| FileRead(e, path.to_path_buf())));
Some(Json::String(buf))
}
_ => None,
};
cfg
Ok(value)
}

fn read_meta_dir(path: &Path) -> Result<Json, MetadataErrors> {
use self::MetadataError::{DirRead, FileNameDecode};
let mut data = BTreeMap::new();
let mut errors = vec!();
match read_dir(path) {
Ok(iter) => {
for entryres in iter {
let entry = match entryres {
Ok(entry) => entry,
Err(e) => {
errors.push(DirRead(e, path.to_path_buf()));
continue;
}
};
let fpath = entry.path();
let stem = fpath.file_stem().and_then(|x| x.to_str());
let extension = fpath.extension().and_then(|x| x.to_str());
if let Some(stem) = stem {
if stem.starts_with(".") {
// Skip hidden files
continue;
}
if let Some(ext) = extension {
match read_meta_entry(&fpath, ext) {
Ok(Some(value)) => {
data.insert(stem.to_string(), value);
}
Ok(None) => {}
Err(e) => {
errors.push(e);
}
}
}
} else {
// Only reason why stem is None in our case is that
// we can't decode filename
errors.push(FileNameDecode(fpath.to_path_buf()));
}
}
}
Err(e) => {
errors.push(DirRead(e, path.to_path_buf()));
}
}
if errors.len() > 0 {
Err(MetadataErrors {
errors: errors,
partial: Json::Object(data),
})
} else {
Ok(Json::Object(data))
}
}

pub fn read_configs(options: &Options, cache: &mut Cache) -> Config {
let meta = read_meta_dir(&options.config_dir.join("machine"));
let roles = HashMap::new();
Config {
machine: meta,
roles: roles,
}
}

impl Config {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn main() {
config.machine.as_ref().ok().and_then(|o| o.as_object())
.map(|x| x.len()).unwrap_or(0),
config.total_errors());
println!("CONFIG {:?}", config);
let mut scheduler = match scheduler::read(&options.config_dir) {
Ok(s) => s,
Err(e) => {
Expand Down
2 changes: 2 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use rustc_serialize::json::Json;
use super::config::Template;


#[derive(Debug)]
pub struct RenderSet {
items: Vec<Renderer>
}

#[derive(Debug)]
pub struct Renderer {
pub source: Template,
pub apply: Command,
Expand Down

0 comments on commit 85df0b4

Please sign in to comment.