config-rs
config-rs copied to clipboard
Re-thinking: Config source: clap
Do we want to support clap as a source of configuration?
This is of lower importance. First we must decide on #323
I built a lib serfig which powered by serde-bridge to make it possible.
The real use-case:
pub fn load() -> Result<Self> {
let arg_conf: Self = Config::parse();
let mut builder: serfig::Builder<Self> = serfig::Builder::default();
// Load from config file first.
{
let config_file = if !arg_conf.config_file.is_empty() {
arg_conf.config_file.clone()
} else if let Ok(path) = env::var("CONFIG_FILE") {
path
} else {
"".to_string()
};
builder = builder.collect(from_file(Toml, &config_file));
}
// Then, load from env.
builder = builder.collect(from_env());
// Finally, load from args.
builder = builder.collect(from_self(arg_conf));
Ok(builder.build()?)
}
Hoping these can help config-rs a bit!
Native support for clap would be great. In Trunk, we currently have config file + env file and lastly clap as input for settings. Most of the file settings can be overriden by clap as well.
At the moment we do all this merging by hand, but config does pretty much the same as we do, so we might want to move to it in the near future. This would be a great convenience to have.