Add initial implementation

This commit is contained in:
Matthias Beyer 2016-01-10 15:04:06 +01:00
parent 79a2377452
commit 9cf3e22636
3 changed files with 266 additions and 2 deletions

View file

@ -0,0 +1,90 @@
use std::default::Default;
use std::fmt::{Debug, Formatter, Error};
use std::path::PathBuf;
pub use config::types::Config;
pub use config::reader::from_file;
pub struct Configuration {
verbosity: bool,
editor: Option<String>,
editor_opts: String,
}
impl Configuration {
pub fn new(rtp: &PathBuf) -> Option<Configuration> {
fetch_config(&rtp).and_then(|cfg| {
let verbosity = cfg.lookup_boolean("verbosity").unwrap_or(false);
let editor = cfg.lookup_str("editor").map(String::from);
let editor_opts = String::from(cfg.lookup_str("editor-opts").unwrap_or(""));
debug!("Building configuration");
debug!(" - verbosity : {:?}", verbosity);
debug!(" - editor : {:?}", editor);
debug!(" - editor-opts: {}", editor_opts);
Some(Configuration {
verbosity: verbosity,
editor: editor,
editor_opts: editor_opts,
})
})
}
}
fn fetch_config(rtp: &PathBuf) -> Option<Config> {
use std::process::exit;
use std::env;
use xdg_basedir;
use itertools::Itertools;
use libimagutil::variants::generate_variants as gen_vars;
let variants = vec!["config", "config.toml", "imagrc", "imagrc.toml"];
let modifier = |base: &PathBuf, v: &'static str| {
let mut base = base.clone();
base.push(format!("/{}", v));
base
};
vec![
gen_vars(rtp.clone(), variants.clone(), &modifier),
env::var("HOME").map(|home| gen_vars(PathBuf::from(home), variants.clone(), &modifier))
.unwrap_or(vec![]),
xdg_basedir::get_data_home().map(|data_dir| gen_vars(data_dir, variants.clone(), &modifier))
.unwrap_or(vec![]),
].iter()
.flatten()
.filter(|path| path.exists())
.map(|path| from_file(&path).ok())
.filter(|loaded| loaded.is_some())
.nth(0)
.map(|inner| inner.unwrap())
}
impl Debug for Configuration {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
try!(write!(f, "Configuration (verbose: {})", self.verbosity));
Ok(())
}
}
impl Default for Configuration {
fn default() -> Configuration {
Configuration {
verbosity: false,
editor: Some(String::from("nano")),
editor_opts: String::from(""),
}
}
}

View file

@ -1,3 +1,147 @@
#[test] #[macro_use] extern crate log;
fn it_works() { #[macro_use] extern crate itertools;
#[cfg(unix)] extern crate xdg_basedir;
extern crate clap;
extern crate config;
extern crate libimagstore;
extern crate libimagutil;
mod configuration;
mod logger;
use std::path::PathBuf;
pub use clap::App;
use log::LogLevelFilter;
use clap::{Arg, ArgMatches};
use configuration::Configuration;
use logger::ImagLogger;
use libimagstore::store::Store;
pub struct Runtime<'a> {
rtp: PathBuf,
configuration: Configuration,
cli_matches: ArgMatches<'a, 'a>,
store: Store,
} }
impl<'a> Runtime<'a> {
/**
* Gets the CLI spec for the program and retreives the config file path (or uses the default on
* in $HOME/.imag/config, $XDG_CONFIG_DIR/imag/config or from env("$IMAG_CONFIG")
* and builds the Runtime object with it.
*
* The cli_spec object should be initially build with the ::get_default_cli_builder() function.
*
*/
pub fn new(cli_spec: App<'a, 'a, 'a, 'a, 'a, 'a>) -> Runtime<'a> {
let matches = cli_spec.get_matches();
let rtp : PathBuf = matches.value_of("runtimepath").unwrap_or("~/.imag/").into();
let storepath = matches.value_of("storepath")
.map(PathBuf::from)
.unwrap_or({
let mut spath = rtp.clone();
spath.push("/store");
spath
});
Runtime {
cli_matches: matches,
configuration: Configuration::new(&rtp).unwrap_or(Configuration::default()),
rtp: rtp,
store: Store::new(storepath),
}
}
/**
* appname should be "imag-foo"
*/
pub fn get_default_cli_builder(appname: &'a str,
version: &'a str,
about: &'a str)
-> App<'a, 'a, 'a, 'a, 'a, 'a>
{
App::new(appname)
.version(version)
.author("Matthias Beyer <mail@beyermatthias.de>")
.about(about)
.arg(Arg::with_name("verbosity")
.short("v")
.long("verbose")
.help("Enables verbosity")
.required(false)
.takes_value(false))
.arg(Arg::with_name("debugging")
.long("debug")
.help("Enables debugging output")
.required(false)
.takes_value(false))
.arg(Arg::with_name("config")
.short("c")
.long("config")
.help("Path to alternative config file")
.required(false)
.takes_value(true))
.arg(Arg::with_name("runtimepath")
.short("r")
.long("rtp")
.help("Alternative runtimepath")
.required(false)
.takes_value(true))
.arg(Arg::with_name("storepath")
.long("store")
.help("Alternative storepath. Must be specified as full path, can be outside of the RTP")
.required(false)
.takes_value(true))
}
pub fn init_logger(&self) {
let lvl = if self.is_debugging() {
LogLevelFilter::Debug
} else if self.is_verbose() {
LogLevelFilter::Info
} else {
LogLevelFilter::Error
};
log::set_logger(|max_log_lvl| {
max_log_lvl.set(lvl);
debug!("Init logger with {}", lvl);
Box::new(ImagLogger::new(lvl.to_log_level().unwrap()))
})
.map_err(|_| {
panic!("Could not setup logger");
})
.ok();
}
pub fn is_verbose(&self) -> bool {
self.cli_matches.is_present("verbosity")
}
pub fn is_debugging(&self) -> bool {
self.cli_matches.is_present("debugging")
}
pub fn rtp(&self) -> &PathBuf {
&self.rtp
}
pub fn cli(&self) -> &ArgMatches {
&self.cli_matches
}
pub fn store(&self) -> &Store {
&self.store
}
}

30
libimagrt/src/logger.rs Normal file
View file

@ -0,0 +1,30 @@
use log::{Log, LogLevel, LogRecord, LogMetadata};
pub struct ImagLogger {
lvl: LogLevel,
}
impl ImagLogger {
pub fn new(lvl: LogLevel) -> ImagLogger {
ImagLogger {
lvl: lvl,
}
}
}
impl Log for ImagLogger {
fn enabled(&self, metadata: &LogMetadata) -> bool {
metadata.level() <= self.lvl
}
fn log(&self, record: &LogRecord) {
if self.enabled(record.metadata()) {
// TODO: This is just simple logging. Maybe we can enhance this lateron
println!("[imag][{: <5}]: {}", record.level(), record.args());
}
}
}