Documentation ¶
Overview ¶
Package cfger provides functions to read configuration files in raw, JSON or YAML format from a file or environment variable. And additionally a shortcut to read docker-secrets. It also follows configuration locations in env-vars if the value has a valid prefix.
Valid prefixes are:
- env:: reads from environment variables.
- secret:: reads file from /run/secrets
- file:: reads file with given path
Raw Versus Structured Read ¶
ReadCfg(val) wraps ReadStructuredCfg(val, nil), which results in a raw read. So if val here resolves to a JSON-file it returns the contents of the JSON-file as a string, without unmarshalling
For cfger to unmarshal the file, you need to supply a valid interface in the form defined in the documentation for the package https://golang.org/pkg/encoding/json/#Unmarshal. Similarly for YAML-unmarshalling you have to supply a valid interface as described in the https://godoc.org/gopkg.in/yaml.v3#Unmarshal documentation.
Index ¶
- func ReadCfg(val string) (string, error)
- func ReadCfgFile(inPath string, structure interface{}) (string, error)
- func ReadEnv(val string, structure interface{}) (string, error)
- func ReadEnvRecursive(val string, structure interface{}) (string, error)
- func ReadStructuredCfg(val string, structure interface{}) (string, error)
- func ReadStructuredCfgFileRecursive(inPath string, structure interface{}) error
- func ReadStructuredCfgRecursive(val string, structure interface{}) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadCfgFile ¶
ReadCfgFile reads the file at the given path and returns the contents as a string. If the suffix of the path is .yml/.yaml/.json the contents are unmarshalled before they are returned. Returns an empty string and an error if an error is returned while reading or unmarshalling.
func ReadEnv ¶
ReadEnv reads an environment-variable by the given name. If the environment-variable contains a prefix the path will be resolved.
func ReadEnvRecursive ¶
ReadEnvRecursive reads an environment variable and proceeds to resolve the value until there the value is a raw value. If the value is a json or yaml-file, the values of the tree will be attempted to be resolved.
func ReadStructuredCfg ¶
ReadStructuredCfg checks if the input has a prefix, e. g. "secret::" or "file::", if it does try to read the file in the given location. In the case of neither the input will be returned. If a prefix is given but an error occurs when reading, an empty string and the error will be returned. If no prefix is given the given val i returned.
Valid prefixes are:
- env:: reads from environment variables.
- secret:: reads file from /run/secrets (a docker secret)
- file:: reads file with given path
Additionally if a file has .yml or .json as a suffix and the given interface{} is not nil, the interface will be used to unmarshal the file at the given path.
If a read environment-variable contains a prefix this function will be called with the environment-variable's value.
func ReadStructuredCfgFileRecursive ¶
ReadStructuredCfgFileRecursive reads the file at the given path, if the path ends in .yml/.yaml or .json, the files contents will be unmarshalled into the given struct. It will then proceed to resolve the value until there the value is a raw value. If the value is a json or yaml-file, the values of the tree will be attempted to be resolved.
func ReadStructuredCfgRecursive ¶
ReadStructuredCfgRecursive reads a prefixed string and proceeds to resolve the value until there the value is a raw value. If the value is a json or yaml-file, the values of the tree will be attempted to be resolved. Example: var config Config _, err := cfger.ReadStructuredCfgRecursive("file::/cfg/cfg.yml", &config)
Types ¶
This section is empty.