-
Notifications
You must be signed in to change notification settings - Fork 3
Layered configuration with command line arguments #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yeah schematic assumes env vars are the highest priority and there's a bit of logic that enforces that. This would be tough to change since this happens in I'd say your best bet (for the interim) is to just manually mutate the final config outside of schematic. let mut loader = ConfigLoader::<AppConfig>::new();
loader.file(config_path)?;
let mut config = loader.load()?.config;
if args.some_value {
config.some_value = args.some_value;
} |
Ah, your comment about finalize gave me an idea. It works if you merge in the final layer after calling let mut loader = ConfigLoader::<AppConfig>::new();
loader.file(config_path)?;
let partial_config = loader.load_partial(&())?;
let mut final_partial_config = partial_config.finalize(&())?;
final_partial_config.merge(&(), cli.config)?;
let config = AppConfig::from_partial(final_partial_config); Guessing this is a slight abuse of I also made a prototype for the first solution that I mentioned regarding passing additional attributes to the generated partial struct. I haven't tested it thoroughly yet, but I'm happy to clean it up and make a PR if you'd be interested. |
Yah I don't think I'll change finalize, but feel free to submit a PR for the derive stuff. |
I just realized that since |
Hey, thanks for making this library. I've been really enjoying it. Recently, I wanted to see if it would be easy to support the common three-layered configuration approach of CLI overrides, environment variables, and config files - in order of decreasing precedence. I think this should be doable with a few tweaks.
I tried using partials with
clap
in order to have a single source of truth for all config values:This doesn't work because the extra annotations (
#[derive(Args)]
plus the#[arg]
attributes) are not propagated to the generatedPartialAppConfig
struct. I think this can work if additional attributes are exposed to allow users to attach arbitrary attributes to the partial struct. Something like this maybe:The second issue I ran into is with precedence. I'm combining the CLI config with the env/file sources like this:
This almost works, but environment variables currently take precedence over the last partial. Some way to specify the priority might work here:
Curious what your thoughts are on this. I'd be happy to work on it if it's something you'd be okay with. Thanks!
The text was updated successfully, but these errors were encountered: