twelf
twelf copied to clipboard
Error: config file missing
Hy, I like your implemantation pretty much and have already mastered to build a configuration which combines everything (defaults, ENV, config.file, CLI). If a field is omitted in the config file I'll get the default or CLI value instead. That is great. But my App is intended to be used mainly on the default values and only in special cases with a different config. So it must be possible to run it without a config file. But if the file is missing or empty I get an error and didn't find a solution. I do not want to create the file and leave it without meaningful entries, if it is not needed. Is there a possibility to omit this layer, if file is not available.
So hasty!
I didn't realize that I could replace the layers with a prebuild vec. In the test files I found my solution.
Now it goes like this and works fine. Only an empty file remains a problem.
// before opening the file with config check if it is available
let mut config_layers = vec![Layer::Env(None),];
if std::path::Path::new(JSON_CONFIG_FILE).exists() {
config_layers.push(Layer::Json(JSON_CONFIG_FILE.into()));
}
config_layers.push(Layer::Clap(matches));
let config = Config::with_layers(&config_layers).unwrap();
Thanks for your work
Hmm interesting, I will try to think about an implementation to enable by default this kind of behavior. Thanks for your feedbacks. If you need anything feel free to ask/create an issue :)