Read Configurations from different sources
To load flags into your configuration struct, just indicate the name of the flag with the flag
tag, flag-usage
is optional
type Conf struct {
Field string `flag:"flag-for-field" flag-usage:"not important"`
}
...
var conf Conf
if err := gonphig.ReadConfig(&conf); err != nil {
log.Fatal(err)
Gonphig will take care of the flag declaration and the flag.Parse()
for you when it reads the configuration
To load env-var into your configuration struct, just indicate the name of the variable with the env
tag
type Conf struct {
Field string `env:"env_variable_to_read"`
}
...
var conf Conf
if err := gonphig.ReadConfig(&conf); err != nil {
log.Fatal(err)
}
You can add a default value to your configuration using the default
tag
type Conf struct {
Field string `default:"someValue"`
AnInt int `default:"42"`
}
The values provided on the file will be taken as the default values
type Conf struct {
Field string
}
...
var conf Conf
if err := gonphig.ReadFromFile("path/to/file.yml", &conf); err != nil {
log.Fatal(err)
}
If you want to rename a field in the yaml file and map it into your config struct, you can use the yaml
tag
type Conf struct {
NotTheSameName string `yaml:"field"`
}
It is possible to add validations over the configuration values using the validate
tag
type Conf struct {
Field string `env:"env_variable_to_read" validate:"required"`
}
- Configuration read from
flags
- Configuration read from
env-var
- Configuration coming from the
default
tag - Configuration read from the
yaml
file
- To perform validations on the configuration struct, We are using an external library. All information can be found here