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

feat: multiple config filepath support. #149

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

Conversation

pansachin
Copy link
Contributor

FEAT

  • If you have a usecase of having different config path based on environment or whatsoever, all the paths can be configured and last valid path will be used to read the configuration.
import "github.com/ilyakaznacheev/cleanenv"

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

var cfg ConfigDatabase

cleanenv.AddConfigPath("config.yml")
cleanenv.AddConfigPath("/dev/config.yml")
cleanenv.AddConfigPath("/prod/config.yml")

err := cleanenv.Read(&cfg)
if err != nil {
    ...
}

This will do the following:

  1. reads last valid path among added paths.
  2. parse configuration file according to YAML format (yaml tag in this case);
  3. reads environment variables and overwrites values from the file with the values which was found in the environment (env tag);
  4. if no value was found on the first two steps, the field will be filled with the default value (env-default tag) if it is set.

@pansachin pansachin changed the title feat: multiple filepath support. feat: multiple config filepath support. Sep 5, 2024
@pansachin
Copy link
Contributor Author

Addressing feature request: #150

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.

1 participant