add hook to retrieve config file path

master
Ondřej Hruška 4 years ago
parent 1455bc97b9
commit d919300b02
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      Cargo.toml
  2. 35
      src/lib.rs

@ -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"

@ -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
));

Loading…
Cancel
Save