Skip to content
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

Add functions for prefixed environment variables #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dkaslovsky
Copy link

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):

type ConfigDatabase struct {
    Port     string `env:"PORT" env-default:"5432"`
    Host     string `env:"HOST" env-default:"localhost"`
    Name     string `env:"NAME" env-default:"postgres"`
    User     string `env:"USER" env-default:"user"`
    Password string `env:"PASSWORD"`
}

The environment variables serving as the configuration for the database application might share a common prefix such as DB or APPNAME (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:

var cfg ConfigDatabase

err := cleanenv.ReadEnvWithPrefix("appname", &cfg)
if err != nil {
    ...
}

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.

@codecov
Copy link

codecov bot commented Dec 12, 2020

Codecov Report

Merging #65 (ee445ab) into master (ce3ee7f) will decrease coverage by 0.14%.
The diff coverage is 91.30%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #65      +/-   ##
==========================================
- Coverage   89.79%   89.65%   -0.15%     
==========================================
  Files           1        1              
  Lines         245      261      +16     
==========================================
+ Hits          220      234      +14     
- Misses         17       19       +2     
  Partials        8        8              
Impacted Files Coverage Δ
cleanenv.go 89.65% <91.30%> (-0.15%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce3ee7f...ee445ab. Read the comment docs.

@batazor
Copy link

batazor commented Jul 26, 2022

@ilyakaznacheev Can we add this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants