Add functions for prefixed environment variables #65
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.
Thanks so much for this repo, I've found it extremely useful!
This PR adds functionality for easily working with environment variables that share a common prefix, as is often the case for the configuration of an application. For example, consider (modified from the README in the current master branch):
The environment variables serving as the configuration for the database application might share a common prefix such as
DB
orAPPNAME
(e.g.,DB_PORT
,DB_HOST
, etc). To cleanly and easily handle this case, rather than repeating the prefix in the struct tags (hindering readability), we add the functions:func ReadConfigWithPrefix(path string, prefix string, cfg interface{}) error
func ReadEnvWithPrefix(prefix string, cfg interface{}) error
func UpdateEnvWithPrefix(prefix string, cfg interface{}) error
so that the prefix is specified once on read. Following the example above, we read the prefixed environment variables as:
For completeness, this PR also adds the following description function:
func GetDescriptionWithPrefix(cfg interface{}, prefix string, headerText *string) (string, error)
This functionality is intended to work similar to the prefix functionality of kelseyhightower/envconfig
Thanks for considering this PR, and I'm more than happy to iterate on any feedback you might have.