Documentation ¶
Overview ¶
Package config handles the configuration of the program. The configuration contains the set of initial parameter settings that are read at run-time by the program. This package allows to load the configuration from a local file, an environment variable or a remote config provider (e.g. Consul, ETCD, Firestore).
Configuration Loading Strategy:
Different entry points can be used during the development, debugging, testing or deployment.
To get the maximum flexibility, the different configuration entry points can be coordinated in the following sequence (1 has the lowest priority and 5 the maximum):
1. In the “myprog” program the configuration parameters are defined as a data structure that can be easily mapped to and from a JSON (or YAML) object, and they are initialized with constant default values;
The program attempts to load the local “config.json” configuration file (or what is specified by defaultConfigName and defaultConfigType) and, as soon one is found, overwrites the values previously set. The configuration file is searched in the following ordered directories: ./ ~/.myprog/ /etc/myprog/
The program attempts to load the environmental variables that define the remote configuration system and, if found, overwrites the correspondent configuration parameters: MYPROG_REMOTECONFIGPROVIDER → remoteConfigProvider MYPROG_REMOTECONFIGENDPOINT → remoteConfigEndpoint MYPROG_REMOTECONFIGPATH → remoteConfigPath MYPROG_REMOTECONFIGSECRETKEYRING → remoteConfigSecretKeyring MYPROG_REMOTECONFIGDATA → remoteConfigData
4. If the remoteConfigProvider parameter is not empty, the program attempts to load the configuration data from the specified source. This can be any remote source supported by the Viper library (e.g. Consul, ETCD) or alternatively from the MYPROG_REMOTECONFIGDATA environment variable as base64 encoded JSON if MYPROG_REMOTECONFIGPROVIDER is set to "envar".
5. Any specified command line property overwrites the correspondent configuration parameter.
6. The configuration parameters are validated via the Validate() function.
An example can be found in examples/service/internal/cli/config.go
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
func Load(cmdName, configDir, envPrefix string, cfg Configuration) error
Load populates the configuration parameters.
Types ¶
type BaseConfig ¶
type BaseConfig struct { // Log configuration. Log LogConfig `mapstructure:"log" validate:"required"` }
BaseConfig contains the default configuration options to be used in the application config struct.
type Configuration ¶
Configuration is the interface we need the application config struct to implement.
type LogConfig ¶
type LogConfig struct { // Level is the standard syslog level: EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG. Level string `mapstructure:"level" validate:"required,oneof=EMERGENCY ALERT CRITICAL ERROR WARNING NOTICE INFO DEBUG"` // Format is the log output format: CONSOLE, JSON. Format string `mapstructure:"format" validate:"required,oneof=CONSOLE JSON"` // Network is the optional network protocol used to send logs via syslog: udp, tcp. Network string `mapstructure:"network" validate:"omitempty,oneof=udp tcp"` // Address is the optional remote syslog network address: (ip:port) or just (:port). Address string `mapstructure:"address" validate:"omitempty,hostname_port"` }
LogConfig contains the configuration for the application logger.
type Viper ¶
type Viper interface { AddConfigPath(in string) AddRemoteProvider(provider, endpoint, path string) error AddSecureRemoteProvider(provider, endpoint, path, secretkeyring string) error AllKeys() []string AutomaticEnv() BindEnv(input ...string) error BindPFlag(key string, flag *pflag.Flag) error Get(key string) interface{} ReadConfig(in io.Reader) error ReadInConfig() error ReadRemoteConfig() error SetConfigName(in string) SetConfigType(in string) SetDefault(key string, value interface{}) SetEnvPrefix(in string) Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error }
Viper is the local interface to the actual viper to allow for mocking.