Documentation ¶
Index ¶
- Variables
- func Load(c interface{}, conf Conf) error
- func LoadMap(c interface{}, vars map[string]interface{}, conf Conf) error
- func LoadRawFile(c interface{}, fileContent []byte, conf Conf) error
- func LoadWithMap(c interface{}, vars map[string]interface{}, conf Conf) error
- func LoadWithRawFile(c interface{}, fileContent []byte, conf Conf) error
- type Conf
- type FileDecoderFn
Constants ¶
This section is empty.
Variables ¶
var DecoderTryAll = NewMultiFileDecoder([]FileDecoderFn{ DecoderYAML, DecoderTOML, DecoderJSON, })
DecoderTryAll is an encoding function that tries all other existing encoding functions and uses the first one that does not produce an error.
The order in which they are tried is: 1. YAML 2. TOML 3. JSON To have them tried in a different order, construct a custom decoder using NewMultiDecoder.
Functions ¶
func Load ¶
Load loads the configuration of your program in the struct at c. Use conf to specify how gonfig should look for configuration variables.
This method can panic if there was a problem in the configuration struct that is used (which should not happen at runtime), but will always try to produce an error instead if the user provided incorrect values.
The recognised tags on the exported struct variables are:
- id: the keyword identifier (defaults to lowercase of variable name)
- default: the default value of the variable
- short: the shorthand used for command line flags (like -h)
- desc: the description of the config var, used in --help
- opts: comma-separated flags. Supported flags are:
- hidden: Hides the option from help outputs.
func LoadMap ¶
LoadWithMap loads the configuration of your program in the struct at c by using the given map. All other config sources will be ignored. Use conf to specify how gonfig should look for configuration variables.
Read documentation of Load for effects.
func LoadRawFile ¶
LoadRawFile loads the configuration of your program in the struct at c from the given raw config file contents. In this method, conf is only used to pass the FileDecoder option. Use conf to specify how gonfig should look for configuration variables.
Read documentation of Load for effects.
func LoadWithMap ¶
LoadWithMap loads the configuration of your program in the struct at c by using the given map. Use conf to specify how gonfig should look for configuration variables. As opposed to LoadMap, in this method, the other config sources are also loaded.
Read documentation of Load for effects.
func LoadWithRawFile ¶
LoadWithRawFile loads the configuration of your program in the struct at c by using the given contents for the config file. Use conf to specify how gonfig should look for configuration variables. As opposed to LoadRawFile, in this method, the other config sources are also loaded.
Read documentation of Load for effects.
Types ¶
type Conf ¶
type Conf struct { // ConfigFileVariable is the config variable that will be read before looking // for a config file. If no value is specified in the environment variables // of the command line flags, the default config file will be read. // This flag should be defined in the config file struct and referred to here // by its ID. The default value for this variable is obviously ignored. ConfigFileVariable string // FileDisable disabled reading config variables from the config file. FileDisable bool // FileDefaultFilename is the default filename to look for for the config // file. If this is empty and no filename is explicitly provided, parsing // a config file is skipped. FileDefaultFilename string // FileDecoder specifies the decoder function to be used for decoding the // config file. The following decoders are provided, but the user can also // specify a custom decoder function: // - DecoderYAML // - DecoderTOML // - DecoderJSON // If no decoder function is provided, gonfig tries to guess the function // based on the file extension and otherwise tries them all in the above // mentioned order. FileDecoder FileDecoderFn // FlagDisable disabled reading config variables from the command line flags. FlagDisable bool // FlagIgnoreUnknown ignores unknown command line flags instead of stopping // with an error message. FlagIgnoreUnknown bool // EnvDisables disables reading config variables from the environment // variables. EnvDisable bool // EnvPrefix is the prefix to use for the the environment variables. // gonfig does not add an underscore after the prefix. EnvPrefix string // HelpDisable disables printing the help message when the --help or -h flag // is provided. If this is false, an explicit --help flag will be added. HelpDisable bool // HelpMessage is the message printed before the list of the flags when the // user sets the --help flag. // The default is "Usage of [executable name]:". HelpMessage string // HelpDescription is the description to print for the help flag. // By default, this is "show this help menu". HelpDescription string }
Conf is used to specify the intended behavior of gonfig.
type FileDecoderFn ¶
FileDecoderFn represents a method that translates the content of a config file to a map[string]interface{}. It is important that in this map, all interface{} types are either: - a simple Go type (intX, uintX, bool, string, floatXX) - a value implementing encoding.TextUnmarshaler - a []byte - a slice of one of those - a map[string]interface{}
var DecoderJSON FileDecoderFn = func(c []byte) (map[string]interface{}, error) { var m map[string]interface{} if err := json.Unmarshal(c, &m); err != nil { return nil, fmt.Errorf("error parsing JSON config file: %v", err) } return m, nil }
DecoderJSON is the JSON decoding function for config files.
var DecoderTOML FileDecoderFn = func(c []byte) (map[string]interface{}, error) { tomlTree, err := toml.LoadBytes(c) if err != nil { return nil, fmt.Errorf("error parsing TOML config file: %v", err) } return tomlTree.ToMap(), nil }
DecoderTOML is the TOML decoding function for config files.
var DecoderYAML FileDecoderFn = func(c []byte) (map[string]interface{}, error) { var m map[string]interface{} if err := yaml.Unmarshal(c, &m); err != nil { return nil, fmt.Errorf("error parsing YAML config file: %v", err) } m = cleanUpYAML(m).(map[string]interface{}) return m, nil }
DecoderYAML is the YAML decoding function for config files.
func NewMultiFileDecoder ¶
func NewMultiFileDecoder(decoders []FileDecoderFn) FileDecoderFn
NewMultiFileDecoder is a hybrid decoders that will try all the given decoders and return the result of the first one that does not produce an error.