Skip to content

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

Attention: Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.65%. Comparing base (ce3ee7f) to head (ee445ab).
Report is 69 commits behind head on master.

Files with missing lines Patch % Lines
cleanenv.go 91.30% 2 Missing ⚠️
Additional details and impacted files

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              
Files with missing lines Coverage Δ
cleanenv.go 89.65% <91.30%> (-0.15%) ⬇️

@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.

2 participants