Implement simple configuration module
This commit is contained in:
parent
53d12a8670
commit
099d63f13a
3 changed files with 62 additions and 28 deletions
|
@ -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/"))
|
||||
}
|
||||
|
||||
|
59
src/configuration.rs
Normal file
59
src/configuration.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
extern crate clap;
|
||||
|
||||
use cli::Config;
|
||||
|
||||
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 verbose : bool,
|
||||
pub debugging : bool,
|
||||
}
|
||||
|
||||
impl Configuration {
|
||||
|
||||
pub fn new(config: &Config) -> Configuration {
|
||||
let rtp = rtp_path(config);
|
||||
|
||||
let mut verbose = false;
|
||||
let mut debugging = false;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Configuration {
|
||||
verbose: verbose,
|
||||
debugging: debugging,
|
||||
rtp: rtp,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_verbose(&self) -> bool {
|
||||
self.verbose
|
||||
}
|
||||
|
||||
pub fn is_debugging(&self) -> bool {
|
||||
self.debugging
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn rtp_path(config: &Config) -> 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()
|
||||
}
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
#[macro_use] extern crate clap;
|
||||
#[macro_use] extern crate log;
|
||||
extern crate config;
|
||||
|
||||
use cli::Config;
|
||||
use config::Configuration;
|
||||
use configuration::Configuration;
|
||||
use runtime::{ImagLogger, Runtime};
|
||||
use clap::App;
|
||||
|
||||
mod cli;
|
||||
mod config;
|
||||
mod configuration;
|
||||
mod runtime;
|
||||
mod module;
|
||||
mod storage;
|
||||
|
|
Loading…
Reference in a new issue