2015-12-05 00:14:13 +00:00
|
|
|
use std::fmt::{Debug, Formatter, Error};
|
2015-10-26 22:51:44 +00:00
|
|
|
use std::path::Path;
|
2015-12-05 00:14:13 +00:00
|
|
|
|
2015-10-26 22:51:44 +00:00
|
|
|
use config::reader::from_file;
|
|
|
|
use config::types::Config as Cfg;
|
2015-12-05 00:14:13 +00:00
|
|
|
use cli::CliConfig;
|
2015-11-27 18:22:21 +00:00
|
|
|
|
2015-12-29 15:51:33 +00:00
|
|
|
/**
|
|
|
|
* Configuration object which represents the configuration file.
|
|
|
|
*
|
|
|
|
* It gets passed a CliConfig object on ::new(), retreives some data from this one which is then
|
|
|
|
* provided as default value to the callee if there is no value for it in the configuration.
|
|
|
|
*
|
|
|
|
* TODO: Setup is kinda ugly, as we re-use data from the CLI, which is the job of the Runtime
|
|
|
|
* object later.
|
|
|
|
*/
|
2015-10-26 22:51:44 +00:00
|
|
|
pub struct Configuration {
|
|
|
|
pub rtp : String,
|
2015-10-26 22:56:54 +00:00
|
|
|
pub store_sub : String,
|
2015-12-30 01:40:38 +00:00
|
|
|
pub editor : Option<String>,
|
2015-12-30 01:45:27 +00:00
|
|
|
pub editor_opts : String,
|
2015-10-26 22:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Configuration {
|
|
|
|
|
2015-10-26 23:02:42 +00:00
|
|
|
pub fn new(config: &CliConfig) -> Configuration {
|
2016-01-02 14:32:47 +00:00
|
|
|
let rtp = rtp_path(config).or(default_path()).unwrap_or(String::from("/tmp/"));
|
|
|
|
|
2015-10-26 22:51:44 +00:00
|
|
|
|
2016-01-02 14:32:47 +00:00
|
|
|
let cfg = fetch_config(&rtp);
|
|
|
|
|
|
|
|
let store_sub = String::from(cfg.lookup_str("store").unwrap_or("/store"));
|
|
|
|
let editor = cfg.lookup_str("editor").map(String::from);
|
|
|
|
let editor_opts = String::from(cfg.lookup_str("editor-opts").unwrap_or(""));
|
2015-12-01 17:32:00 +00:00
|
|
|
|
|
|
|
debug!("Building configuration");
|
|
|
|
debug!(" - store sub : {}", store_sub);
|
2016-01-02 14:32:47 +00:00
|
|
|
debug!(" - runtimepath: {}", rtp);
|
2015-12-30 02:02:19 +00:00
|
|
|
debug!(" - editor : {:?}", editor);
|
2015-12-30 01:45:27 +00:00
|
|
|
debug!(" - editor-opts: {}", editor_opts);
|
2015-12-01 17:32:00 +00:00
|
|
|
|
2015-10-26 22:51:44 +00:00
|
|
|
Configuration {
|
2015-10-26 22:56:54 +00:00
|
|
|
store_sub: store_sub,
|
2016-01-02 14:32:47 +00:00
|
|
|
rtp: rtp,
|
2015-12-30 01:40:38 +00:00
|
|
|
editor: editor,
|
2015-12-30 01:45:27 +00:00
|
|
|
editor_opts: editor_opts,
|
2015-10-26 22:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:51:33 +00:00
|
|
|
/**
|
|
|
|
* Get the store path the configuration configured
|
|
|
|
*/
|
2015-12-01 17:36:04 +00:00
|
|
|
pub fn store_path(&self) -> String {
|
2015-10-26 22:56:54 +00:00
|
|
|
format!("{}{}", self.rtp, self.store_sub)
|
|
|
|
}
|
|
|
|
|
2015-12-29 15:51:33 +00:00
|
|
|
/**
|
|
|
|
* Get the runtime path the configuration configured
|
|
|
|
*/
|
2015-11-23 18:48:33 +00:00
|
|
|
pub fn get_rtp(&self) -> String {
|
|
|
|
self.rtp.clone()
|
|
|
|
}
|
|
|
|
|
2015-12-30 01:40:38 +00:00
|
|
|
pub fn editor(&self) -> Option<String> {
|
2015-12-30 02:02:19 +00:00
|
|
|
self.editor.clone()
|
2015-12-30 01:40:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 01:45:27 +00:00
|
|
|
pub fn editor_opts(&self) -> &String {
|
|
|
|
&self.editor_opts
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 15:51:33 +00:00
|
|
|
/**
|
|
|
|
* Helper to get the runtimepath from the CLI
|
|
|
|
*/
|
2015-12-01 17:19:57 +00:00
|
|
|
fn rtp_path(config: &CliConfig) -> Option<String> {
|
|
|
|
config.cli_matches.value_of("rtp")
|
|
|
|
.and_then(|s| Some(String::from(s)))
|
|
|
|
}
|
|
|
|
|
2016-01-02 14:32:47 +00:00
|
|
|
fn fetch_config(rtp: &String) -> Cfg {
|
|
|
|
use std::process::exit;
|
|
|
|
|
|
|
|
let configpath = format!("{}{}", rtp, "/config");
|
|
|
|
from_file(Path::new(&configpath)).map_err(|e| {
|
|
|
|
println!("Error loading config at '{}' -> {:?}", configpath, e);
|
|
|
|
println!("Exiting now.");
|
|
|
|
exit(1)
|
|
|
|
}).unwrap()
|
2015-10-26 22:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 15:51:33 +00:00
|
|
|
/**
|
|
|
|
* Default runtime path, if available.
|
|
|
|
*/
|
2015-12-01 17:19:57 +00:00
|
|
|
fn default_path() -> Option<String> {
|
|
|
|
use std::env::home_dir;
|
|
|
|
|
|
|
|
home_dir().and_then(|mut buf| {
|
|
|
|
buf.push("/.imag");
|
|
|
|
buf.to_str().map(|s| String::from(s))
|
|
|
|
})
|
|
|
|
|
2015-10-26 22:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 18:22:21 +00:00
|
|
|
impl Debug for Configuration {
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
2016-01-02 14:39:57 +00:00
|
|
|
write!(f, "Configuration (rtp: {}, store path: {})",
|
2015-11-27 18:22:21 +00:00
|
|
|
self.get_rtp(),
|
2015-12-01 17:36:04 +00:00
|
|
|
self.store_path()
|
2015-11-27 18:22:21 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|