diff --git a/config-parser/config-parser-common/src/common.rs b/config-parser/config-parser-common/src/common.rs index 9f263d5..db2ad93 100644 --- a/config-parser/config-parser-common/src/common.rs +++ b/config-parser/config-parser-common/src/common.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use std::{collections::HashMap, ops::IndexMut}; +use std::collections::HashMap; /// Holds config, value is either a `String` or a nested `ConfigMap` pub type ConfigMap = HashMap; @@ -13,6 +13,8 @@ pub enum ConfigElement { Nested(ConfigMap), } +/// parse toml'ish file into a structure (`ConfigMap`) +/// as an intermediate step to parsing the configuration pub fn parse_config_file(input: &String) -> (ConfigMap, Vec) { let mut config = ConfigMap::new(); let mut pointer: &mut ConfigMap = &mut config; diff --git a/src/config.rs b/src/config.rs index fbbb296..77fdffb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -129,9 +129,7 @@ impl Config { let mut config_file = match config_dir() { Some(value) => value, None => { - return Err(Error::other( - "-- failed to retrieve configuration directory", - )) + return Err(Error::other("-- failed to retrieve configuration directory")) } }; // expand path to configuration file and default configuration file @@ -143,7 +141,7 @@ impl Config { if !default_file.is_file() { let mut default_string = Self::DEFAULT_FILE_HEADER.to_string(); default_string.push_str(&config.to_string()); - _ = fs::write(default_file, default_string); + _ = fs::write(&default_file, default_string); } // parse configuration file and populate config struct @@ -154,6 +152,13 @@ impl Config { Err(error) => return Err(error), }; _ = config.parse_from_string(&config_str); + } else { + let default_config = match fs::read_to_string(&default_file) { + Ok(value) => value, + Err(error) => return Err(error), + }; + _ = fs::write(&config_file, &default_config); + _ = config.parse_from_string(&default_config); } if styles_as_ansi_sequences { diff --git a/src/main.rs b/src/main.rs index dc83c5d..513f33b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,8 @@ fn main() -> Result<()> { Err(error) => { // config object is not ready at this point so the style // has to be created by hand - print!("echo -e '{}{}{}' && false", generate_style_sequence(None, Some(COLORS.fg.red), None), error, RESET_SEQ); + output.push_error(&error.to_string()); + output.print_output(None); return Ok(()) } }; @@ -45,7 +46,7 @@ fn main() -> Result<()> { output.push_error(&error.to_string()); }, } - output.print_output(&config); + output.print_output(Some(&config)); return Ok(()); } }; @@ -53,7 +54,7 @@ fn main() -> Result<()> { Ok(value) => value, Err(error) => { output.push_error(&error.to_string()); - output.print_output(&config); + output.print_output(Some(&config)); return Ok(()); } }; @@ -78,7 +79,7 @@ fn main() -> Result<()> { } // print output and command - output.print_output(&config); + output.print_output(Some(&config)); Ok(()) } diff --git a/src/output.rs b/src/output.rs index ea078b9..98d72c1 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,5 +1,7 @@ #![allow(unused)] +use clap::builder::EnumValueParser; + use super::config::*; use std::backtrace::Backtrace; @@ -56,7 +58,13 @@ impl Output { /// format and print styled output /// NOTE - this will execute any commands held by `command` - pub fn print_output(&self, config: &Config) { + pub fn print_output(&self, config: Option<&Config>) { + let default = Config::default(); + let config = if let Some(value) = config { + value + } else { + &default + }; let mut info: String = self.info.iter().map(|entry| format!("echo '{}'", entry)).collect::>().join(" && "); let mut warning: String = self.warning.iter().map(|entry| format!("echo '{}'", entry)).collect::>().join(" && "); let mut error: String = self.error.iter().map(|entry| format!("echo '{}'", entry)).collect::>().join(" && ");