Documentation
¶
Overview ¶
Package cfgloader provides a wrapper for the koanf/v2 configuration package that make it easier to use for a preferred use case, configuring it to load values (in order) from yaml files, flags and environment variables.
Values are loaded in order from: - configuration files in the order given on the command line - environment variables - flags
If multiple sources contain different values for the same configruation field, the last one found is used.
In order for a structure field to be loaded, it must be exported (e.g start with a capital letter), and contain a koanf field tag:
type Config struct { Value string `koanf:"value"` }
Values are loaded from all the sources based on the tag, and would be loaded from any of these sources if the were supplied:
$ export VALUE="from the environment" or $ ./prog -value="from a flag" or $ cat config.yaml value: from a config file
There are two strings that affect the loading of the configuration. There is a prefix which can be used to avoid collisons with environment variables.
If the prefix is set to "CONFIG_" when calling New, then the above example becomes:
$ export CONFIG_VALUE="from the environment"
Delimiter is used to separate nested configuration structures in flags:
type Nested struct { Val int `koanf:"val"` } type Config struct { Value string `koanf:"value"` Nested Nested `koanf:"nested"` }
With the delimiter set to ".", to set val, use: $ ./prog --nested.val=7
Environment variables always use "_" as their delimter, so without a prefix this would be:
$ export NESTED_VAL="from the environment"
Notes:
- This requires using the pflags package instead of the built in flags package.
- In order for flags to work, the flagset's Parse() routine must be called before calling Load()
- There are case sensitivities between the koanf struct tag, the flag name and the JSON field names. Some combinations work, but it is easiest to make them all match.
Index ¶
Constants ¶
const FileArgName = "config"
FileArgName is the name that is used to specify configuration files.
Variables ¶
var (
BadDelimiterError = errors.New("delimiter must contain exactly 1 character")
)
Functions ¶
This section is empty.