cfger

package module
v0.0.0-...-4b67bfd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 8 Imported by: 0

README

cfger

Go-package to read structured or unstructured configuration from environment variables, files and docker secrets.

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:

  1. env:: reads from environment variables.
  2. secret:: reads file from /run/secrets
  3. 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadCfg

func ReadCfg(val string) (string, error)

ReadCfg reads an unstructured configuration-value. See ReadStructuredCfg.

func ReadCfgFile

func ReadCfgFile(inPath string, structure interface{}) (string, error)

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

func ReadEnv(val string, structure interface{}) (string, error)

ReadEnv reads an environment-variable by the given name. If the environment-variable contains a prefix the path will be resolved.

func ReadEnvRecursive

func ReadEnvRecursive(val string, structure interface{}) (string, error)

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

func ReadStructuredCfg(val string, structure interface{}) (string, error)

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:

  1. env:: reads from environment variables.
  2. secret:: reads file from /run/secrets (a docker secret)
  3. 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

func ReadStructuredCfgFileRecursive(inPath string, structure interface{}) error

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

func ReadStructuredCfgRecursive(val string, structure interface{}) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL