Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
use liboverdrop crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Fairley committed Jun 10, 2019
1 parent ee66044 commit 423d034
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ clap = "2.33"
env_logger = "^0.6.1"
failure = "^0.1.5"
glob = "^0.3.0"
liboverdrop = { git = "https://github.com/rfairley/liboverdrop-rs", branch = "rfairley-initial-lib" }
log = "^0.4.6"
serde = { version = "^1.0.91", features = ["derive"] }
toml = "^0.5.1"
42 changes: 8 additions & 34 deletions src/config/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,8 @@
use crate::config::fragments;

use failure::{bail, ResultExt};
use log::debug;
use serde::Serialize;
use std::{collections, fs, path};

/// Read dir and add file (name, path) keys to tree.
fn add_snippets_to_tree(
dir: &path::PathBuf,
tree: &mut collections::BTreeMap<String, path::PathBuf>
) -> failure::Fallible<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
debug!("found fragment '{}'", path.display());

if !path.is_dir() && path.extension().unwrap() == "toml" {
let key = path.file_name().unwrap().to_str().unwrap().to_owned();
if !tree.contains_key(&key) {
debug!("adding fragment with filename '{}' to config", key);
tree.insert(key, path);
}
}
}
}
Ok(())
}
use std::{collections, path};

#[derive(Debug, Serialize)]
pub(crate) struct ConfigInput {
Expand All @@ -40,17 +16,15 @@ pub(crate) struct ConfigInput {
impl ConfigInput {
/// Read config fragments and merge them into a single config.
pub(crate) fn read_configs(
dirs: &[path::PathBuf],
dirs: &Vec<String>,
app_name: &str
) -> failure::Fallible<Self> {
let mut fragments = collections::BTreeMap::new();
for prefix in dirs {
let dir = path::PathBuf::from(format!("{}/{}/config.d", prefix.as_path().display(), app_name));
debug!("scanning configuration directory '{}'", dir.display());

add_snippets_to_tree(&dir, &mut fragments)?;
}

let config_path = format!("{}/config.d", app_name);
let od_cfg = liboverdrop::OverdropConf::new(dirs, config_path.as_str());
let allowed_extensions = vec![
"toml",
];
let fragments = od_cfg.scan_unique_files(false, Some(&allowed_extensions))?;
let cfg = Self::merge_fragments(fragments)?;

cfg.validate_input()?;
Expand Down
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use clap::{Arg, crate_authors, crate_description, crate_name, crate_version};
use config::inputs;
use failure::{bail, ResultExt};
use log::LevelFilter;
use path::PathBuf;
use std::path;

/// Parse the reporting.enabled and collecting.level keys from config fragments,
/// and check that the keys are set to a valid telemetry setting. If not,
Expand Down Expand Up @@ -47,9 +45,9 @@ fn main() -> failure::Fallible<()> {
.try_init()?;

let dirs = vec![
PathBuf::from("/etc"),
PathBuf::from("/run"),
PathBuf::from("/usr/lib"),
String::from("/usr/lib"),
String::from("/run"),
String::from("/etc"),
];
let config = inputs::ConfigInput::read_configs(&dirs, crate_name!())
.context("failed to read configuration input")?;
Expand Down

0 comments on commit 423d034

Please sign in to comment.