config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

Re-thinking: Config source: clap

Open matthiasbeyer opened this issue 3 years ago • 3 comments

Do we want to support clap as a source of configuration?


This is of lower importance. First we must decide on #323

matthiasbeyer avatar Apr 23 '22 07:04 matthiasbeyer

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!

Xuanwo avatar Apr 24 '22 10:04 Xuanwo

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.

dnaka91 avatar May 01 '22 14:05 dnaka91