goconfig

package module
v0.0.0-...-04bd7a1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2024 License: MIT Imports: 10 Imported by: 0

README

config

Package config provides wrapper 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.

See the go documenation for usage details.

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

View Source
const FileArgName = "config"

FileArgName is the name that is used to specify configuration files.

Variables

View Source
var (
	BadDelimiterError = errors.New("delimiter must contain exactly 1 character")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config holds the data necessary to process configuration data.

func New

func New(envPrefix, flagDelimiter string) (Config, error)

New returns a Config initialized with prefix and delimiter. For information about how these values are used see the description of load.

func (Config) Load

func (c Config) Load(f *pflag.FlagSet, cfg interface{}) error

Load loads values into cfg from environment variables, flags and json files.

Jump to

Keyboard shortcuts

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