Documentation ¶
Overview ¶
Package conf package provides tools for easily loading program configurations from multiple sources such as the command line arguments, environment, or a configuration file.
Most applications only need to use the Load function to get their settings loaded into an object. By default, Load will read from a configurable file defined by the -config-file command line argument, load values present in the environment, and finally load the program arguments.
The object in which the configuration is loaded must be a struct, the names and types of its fields are introspected by the Load function to understand how to load the configuration.
The name deduction from the struct field obeys the same rules than those implemented by the standard encoding/json package, which means the program can set the "conf" tag to override the default field names in the command line arguments and configuration file.
A "help" tag may also be set on the fields of the configuration object to add documentation to the setting, which will be shown when the program is asked to print its help.
When values are loaded from the environment the Load function looks for variables matching the struct fields names in snake-upper-case form.
Index ¶
- Variables
- func EqualNode(n1 Node, n2 Node) bool
- func Load(cfg interface{}) (args []string)
- func LoadWith(cfg interface{}, ld Loader) (cmd string, args []string)
- func SetPPROF(config PPROF)
- type Array
- func (a Array) DecodeValue(d objconv.Decoder) (err error)
- func (a Array) EncodeValue(e objconv.Encoder) (err error)
- func (a Array) Item(i int) Node
- func (a Array) Items() []Node
- func (a Array) Kind() NodeKind
- func (a Array) Len() int
- func (a Array) Set(s string) error
- func (a Array) String() string
- func (a Array) Type() string
- func (a Array) Value() interface{}
- type Command
- type FlagSource
- type Loader
- type Map
- func (m Map) DecodeValue(d objconv.Decoder) error
- func (m Map) EncodeValue(e objconv.Encoder) error
- func (m Map) Item(name string) Node
- func (m Map) Items() []MapItem
- func (m Map) Kind() NodeKind
- func (m Map) Len() int
- func (m Map) Scan(do func([]string, MapItem))
- func (m Map) Set(s string) error
- func (m Map) String() string
- func (m Map) Type() string
- func (m Map) Value() interface{}
- type MapItem
- type Node
- type NodeKind
- type PPROF
- type Scalar
- func (s Scalar) DecodeValue(d objconv.Decoder) error
- func (s Scalar) EncodeValue(e objconv.Encoder) error
- func (s Scalar) IsBoolFlag() bool
- func (s Scalar) Kind() NodeKind
- func (s Scalar) Set(str string) (err error)
- func (s Scalar) String() string
- func (s Scalar) Type() string
- func (s Scalar) Value() interface{}
- type Source
- type SourceFunc
Constants ¶
This section is empty.
Variables ¶
var ( // Modifier is the default modification lib using the "mod" tag; it is // exposed to allow registering of custom modifiers and aliases or to // be set to a more central instance located in another repo. Modifier = modifiers.New() )
Functions ¶
func Load ¶
func Load(cfg interface{}) (args []string)
Load the program's configuration into cfg, and returns the list of leftover arguments.
The cfg argument is expected to be a pointer to a struct type where exported fields or fields with a "conf" tag will be used to load the program configuration. The function panics if cfg is not a pointer to struct, or if it's a nil pointer.
The configuration is loaded from the command line, environment and optional configuration file if the -config-file option is present in the program arguments.
Values found in the progrma arguments take precedence over those found in the environment, which takes precedence over the configuration file.
If an error is detected with the configurable the function print the usage message to stderr and exit with status code 1.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array is a node type that wraps a slice value.
type Command ¶
type Command struct { Name string // name of the command Help string // help message describing what the command does }
A Command represents a command supported by a configuration loader.
type FlagSource ¶
type FlagSource interface { Source // Flag is the name of the flag that sets the source's configuration value. Flag() string // Help is called to get the help message to display for the source's flag. Help() string // flag.Value must be implemented by a FlagSource to receive their value // when the loader's arguments are parsed. flag.Value }
FlagSource is a special case of a source that receives a configuration value from the arguments of a loader. It makes it possible to provide runtime configuration to the source from the command line arguments of a program.
func NewFileSource ¶
func NewFileSource(flag string, vars interface{}, readFile func(string) ([]byte, error), unmarshal func([]byte, interface{}) error) FlagSource
NewFileSource creates a new source which loads a configuration from a file identified by a path (or URL).
The returned source satisfies the FlagSource interface because it loads the file location from the given flag.
The vars argument may be set to render the configuration file if it's a template.
The readFile function loads the file content in-memory from a file location given as argument, usually this is ioutil.ReadFile.
The unmarshal function decodes the content of the configuration file into a configuration object.
type Loader ¶
type Loader struct { Name string // program name Usage string // program usage Args []string // list of arguments Commands []Command // list of commands Sources []Source // list of sources to load configuration from. }
A Loader exposes an API for customizing how a configuration is loaded and where it's loaded from.
func (Loader) FprintError ¶
FprintError outputs the error message for err to w.
func (Loader) FprintHelp ¶
FprintHelp outputs the help message for cfg to w.
func (Loader) Load ¶
Load uses the loader ld to load the program configuration into cfg, and returns the list of program arguments that were not used.
The function returns flag.ErrHelp when the list of arguments contained -h, -help, or --help.
The cfg argument is expected to be a pointer to a struct type where exported fields or fields with a "conf" tag will be used to load the program configuration. The function panics if cfg is not a pointer to struct, or if it's a nil pointer and no commands were set.
func (Loader) PrintError ¶
PrintError outputs the error message for err to stderr.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a map type that wraps a map or struct value.
type Node ¶
type Node interface { flag.Value objconv.ValueEncoder objconv.ValueDecoder // Kind returns the NodeKind of the configuration node. Kind() NodeKind // Value returns the underlying value wrapped by the configuration node. Value() interface{} }
The Node interface defines the common interface supported by the different types of configuration nodes supported by the conf package.
type NodeKind ¶
type NodeKind int
NodeKind is an enumeration which describes the different types of nodes that are supported in a configuration.
type PPROF ¶
type PPROF struct { BlockProfileRate int `` /* 164-byte string literal not displayed */ MutexProfileFraction int `` /* 164-byte string literal not displayed */ }
PPROF is a configuration struct which can be used to configure the runtime profilers of programs.
config := struct{ PPROF `conf:"pprof"` }{ PPROF: conf.DefaultPPROF(), } conf.Load(&config) conf.SetPPROF(config.PPROF)
func DefaultPPROF ¶
func DefaultPPROF() PPROF
DefaultPPROF returns the default value of a PPROF struct. Note that the zero-value is valid, DefaultPPROF differs because it captures the current configuration of the program's runtime.
type Scalar ¶
type Scalar struct {
// contains filtered or unexported fields
}
A Scalar is a node type that wraps a basic value.
func (Scalar) IsBoolFlag ¶
type Source ¶
Source is the interface that allow new types to be plugged into a loader to make it possible to load configuration from new places.
When the configuration is loaded the Load method of each source that was set on a loader is called with an Node representating the configuration struct. The typical implementation of a source is to load the serialized version of the configuration and use an objconv decoder to build the node.
func NewEnvSource ¶
NewEnvSource creates a new source which loads values from the environment variables given in env.
A prefix may be set to namespace the environment variables that the source will be looking at.
type SourceFunc ¶
SourceFunc makes it possible to use basic function types as configuration sources.