Add support for environment substitution within config files #706
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Coming from a Spring background, I'm missing the possibility of expansion of environment variables referenced from within configuration files, giving an even more flexible approach next to overriding properties which is in itself a great functionality of the config crate. Using this proposed change, there is no requirement to fully-qualify environment variable names that override the config files.
Spring also has support for a default value.
Both elements have been implemented in this PR, where :- splits the environment variable from the default, e.g.:
Due to current design, this had to be implemented in the file format files.
It's currently implemented as a feature in order not to break existing code. The feature is substitute_env
Although there are some crates that could be used like shellexpand and envsubst this was the easiest way to prevent more dependencies.
The implementation is done by creating an extension function by means of trait ExpandEnvVars which has a default behavior if the feature is not activated.
The trait impl has its own tests. Furthermore, base upon the existing yaml and toml tests soms simple tests have been provided in [file_with_env_vars.rs.]
Would you mind considering the proposal? I'd be glad to head from you.