|
|
|
@ -48,6 +48,12 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { |
|
|
|
|
clap |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Called when the config file is resolved, e.g. to store it in Config
|
|
|
|
|
/// for later relative path resolution
|
|
|
|
|
fn on_config_file_found(&mut self, _path : &PathBuf) { |
|
|
|
|
//
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Configure the config object using args.
|
|
|
|
|
/// Logging has already been inited and configured.
|
|
|
|
|
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> Fallible<Self::Init>; |
|
|
|
@ -119,15 +125,32 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default { |
|
|
|
|
|
|
|
|
|
let filepath = PathBuf::from_str(cfg_file)?; |
|
|
|
|
|
|
|
|
|
let config: Self = if filepath.is_file() { |
|
|
|
|
let path = filepath.canonicalize()?; |
|
|
|
|
Self::pre_log_println(format!("Loading config from: {}", path.display())); |
|
|
|
|
let config = if filepath.exists() { |
|
|
|
|
let mut path = filepath.canonicalize()?; |
|
|
|
|
|
|
|
|
|
if path.is_dir() { |
|
|
|
|
path = path.join(cfg_file_name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if path.exists() { |
|
|
|
|
Self::pre_log_println(format!("Loading config from: {}", path.display())); |
|
|
|
|
|
|
|
|
|
let buf = read_file(&path)?; |
|
|
|
|
|
|
|
|
|
let buf = read_file(path)?; |
|
|
|
|
json5::from_str(&buf)? |
|
|
|
|
let mut config: Self = json5::from_str(&buf)?; |
|
|
|
|
config.on_config_file_found(&path); |
|
|
|
|
config |
|
|
|
|
} else { |
|
|
|
|
Self::pre_log_println(format!( |
|
|
|
|
"Config file \"{}\" does not exist, using defaults.", |
|
|
|
|
path.display() |
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
Self::default() |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
Self::pre_log_println(format!( |
|
|
|
|
"Config file \"{}\" does not exist, using defaults.", |
|
|
|
|
"Config path \"{}\" does not exist, using defaults.", |
|
|
|
|
cfg_file |
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|