writes default if no config file found
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::{collections::HashMap, ops::IndexMut};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Holds config, value is either a `String` or a nested `ConfigMap`
|
/// Holds config, value is either a `String` or a nested `ConfigMap`
|
||||||
pub type ConfigMap = HashMap<String, ConfigElement>;
|
pub type ConfigMap = HashMap<String, ConfigElement>;
|
||||||
@@ -13,6 +13,8 @@ pub enum ConfigElement {
|
|||||||
Nested(ConfigMap),
|
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<String>) {
|
pub fn parse_config_file(input: &String) -> (ConfigMap, Vec<String>) {
|
||||||
let mut config = ConfigMap::new();
|
let mut config = ConfigMap::new();
|
||||||
let mut pointer: &mut ConfigMap = &mut config;
|
let mut pointer: &mut ConfigMap = &mut config;
|
||||||
|
|||||||
+9
-4
@@ -129,9 +129,7 @@ impl Config {
|
|||||||
let mut config_file = match config_dir() {
|
let mut config_file = match config_dir() {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => {
|
None => {
|
||||||
return Err(Error::other(
|
return Err(Error::other("-- failed to retrieve configuration directory"))
|
||||||
"-- failed to retrieve configuration directory",
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// expand path to configuration file and default configuration file
|
// expand path to configuration file and default configuration file
|
||||||
@@ -143,7 +141,7 @@ impl Config {
|
|||||||
if !default_file.is_file() {
|
if !default_file.is_file() {
|
||||||
let mut default_string = Self::DEFAULT_FILE_HEADER.to_string();
|
let mut default_string = Self::DEFAULT_FILE_HEADER.to_string();
|
||||||
default_string.push_str(&config.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
|
// parse configuration file and populate config struct
|
||||||
@@ -154,6 +152,13 @@ impl Config {
|
|||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
};
|
};
|
||||||
_ = config.parse_from_string(&config_str);
|
_ = 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 {
|
if styles_as_ansi_sequences {
|
||||||
|
|||||||
+5
-4
@@ -27,7 +27,8 @@ fn main() -> Result<()> {
|
|||||||
Err(error) => {
|
Err(error) => {
|
||||||
// config object is not ready at this point so the style
|
// config object is not ready at this point so the style
|
||||||
// has to be created by hand
|
// 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(())
|
return Ok(())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -45,7 +46,7 @@ fn main() -> Result<()> {
|
|||||||
output.push_error(&error.to_string());
|
output.push_error(&error.to_string());
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
output.print_output(&config);
|
output.print_output(Some(&config));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -53,7 +54,7 @@ fn main() -> Result<()> {
|
|||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
output.push_error(&error.to_string());
|
output.push_error(&error.to_string());
|
||||||
output.print_output(&config);
|
output.print_output(Some(&config));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -78,7 +79,7 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// print output and command
|
// print output and command
|
||||||
output.print_output(&config);
|
output.print_output(Some(&config));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-1
@@ -1,5 +1,7 @@
|
|||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use clap::builder::EnumValueParser;
|
||||||
|
|
||||||
use super::config::*;
|
use super::config::*;
|
||||||
|
|
||||||
use std::backtrace::Backtrace;
|
use std::backtrace::Backtrace;
|
||||||
@@ -56,7 +58,13 @@ impl Output {
|
|||||||
|
|
||||||
/// format and print styled output
|
/// format and print styled output
|
||||||
/// NOTE - this will execute any commands held by `command`
|
/// 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::<Vec<String>>().join(" && ");
|
let mut info: String = self.info.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
||||||
let mut warning: String = self.warning.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
let mut warning: String = self.warning.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
||||||
let mut error: String = self.error.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
let mut error: String = self.error.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
||||||
|
|||||||
Reference in New Issue
Block a user