diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..2dd1c5c --- /dev/null +++ b/src/config.rs @@ -0,0 +1,82 @@ +use std::{ + fs::read_to_string, + env::{current_dir, var}, + path::Path +}; + +use configparser::ini::Ini; + +pub struct Config { + pub local: Option, + pub global: Option +} + +impl Config { + pub fn new() -> Config { + // Instantiate global config + let i_dir_global = String::from(var("HOME").ok().unwrap()); + let dir_global = Path::new(i_dir_global.as_str()); + let i_path_global = dir_global.join(".config/open.conf"); + let path_global = i_path_global.as_path(); + let mut global: Option = None; + if path_global.exists() { + let i_global = read_to_string(path_global).unwrap(); + let mut tmp = Ini::new(); + tmp.read(i_global).ok(); + global = Some(tmp); + } + + // Instantiate local config, if it exists. + let i_dir_local = current_dir().unwrap(); + let mut dir_local = i_dir_local.as_path(); + let mut i_path_local = dir_local.join(".open"); + let mut path_local = i_path_local.as_path(); + let root = Path::new("/"); + let mut local: Option = None; + while !path_local.exists() { + if dir_local == root { + break; + } + dir_local = dir_local.parent().unwrap(); + i_path_local = dir_local.join(".open"); + path_local = i_path_local.as_path(); + if path_local.exists() { + let i_local = read_to_string(path_local).unwrap(); + let mut tmp = Ini::new(); + tmp.read(i_local).ok(); + local = Some(tmp); + } + } + + if global.is_none() && local.is_none() { + panic!("No configuration found."); + } + + // Prepare loop condition + let output = Config { + global, + local + }; + return output; + } + pub fn get(&self, section: &str, key: &str) -> Option { + let mut output: Option = None; + if self.local.is_some() { + output = self.local.clone().unwrap().get(section, key); + } + if output.is_none() && self.global.is_some() { + output = self.global.clone().unwrap().get(section, key); + } + return output; + } + pub fn getbool(&self, section: &str, key: &str) -> Result, String> { + let mut output = Ok(None); + if self.local.is_some() { + output = self.local.clone().unwrap().getbool(section, key); + } + if output.clone().ok().is_none() && self.global.is_some() { + output = self.global.clone().unwrap().getbool(section, key); + } + return output; + } +} diff --git a/src/main.rs b/src/main.rs index c295fcc..b93ffc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,42 +1,17 @@ use std::{ - fs::read_to_string, - env::{args, current_dir, var}, + env::{args, current_dir}, path::Path, process::{Command, Stdio} }; -use configparser::ini::Ini; +mod config; +use config::Config; fn main() { // Prepare config file let i_dir = current_dir().unwrap(); - let mut dir = i_dir.as_path(); - let mut i_path = dir.join(".open"); - let mut path = i_path.as_path(); - let root = Path::new("/"); - while !path.exists() { - if dir == root { - // If we hit root while propagating, default to the - // user config. - let i_dir = String::from(var("HOME").ok().unwrap()); - dir = Path::new(i_dir.as_str()); - i_path = dir.join(".config/open.conf"); - path = i_path.as_path(); - if path.exists() { - break; - } else { - println!("No configuration found."); - return; - } - } - dir = dir.parent().unwrap(); - i_path = dir.join(".open"); - path = i_path.as_path(); - } - let ini_str = read_to_string(path).unwrap(); - let mut config = Ini::new(); - config.read(ini_str).ok(); - dir = i_dir.as_path(); + let dir = i_dir.as_path(); + let config = Config::new(); let args: Vec = args().collect();