add hook to retrieve config file path
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "clappconfig"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
|
||||
edition = "2018"
|
||||
license = "MIT"
|
||||
|
||||
+29
-6
@@ -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()?;
|
||||
|
||||
let buf = read_file(path)?;
|
||||
json5::from_str(&buf)?
|
||||
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 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
|
||||
));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user