Documentation
¶
Overview ¶
ezconfig is an opinionated wrapper around Viper and pflag. It allows the autoregistration of config files, flags and environement variables simultaneously. It supports registering a struct so that its values can also be set via flags or env values.
Example ¶
// Tell it where to look
SetFile("config", "./testdata")
SetEnvPrefix("TESTER")
// mock environment variables
os.Setenv("TESTER_SUPER_DEEP_FOO", "bar")
// register with the system
var super testdata.FullExample
Bind("super", &super)
// once everything set, just run:
Initialize()
fmt.Println(super.Simple)
fmt.Println(viper.GetString("super.deep.foo"))
fmt.Println(viper.GetString("shallow.foo"))
Output: untouched bar just barely here
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶ added in v0.2.0
func Bind(key string, obj interface{})
Bind parses a struct definition and autobinds pflags based on the struct 'json' tag. An optional prefix is added to all found names in order to support nesting structs. It also supports an optional 'usage' tag.
Example ¶
viper.Reset()
Bind("simple", testdata.SmallExample{})
fmt.Println(pflag.Lookup("simple-none").Usage)
fmt.Println(viper.GetBool("simple.none"))
Output: A boolean flag false
func Initialize ¶ added in v0.2.0
func Initialize() error
func SetEnvPrefix ¶
func SetEnvPrefix(prefix string)
SetEnvPrefix tells Viper to check environment variables. It automatically adds the prefix and sets '_' as the seperator.
func SetFile ¶
SetFile tells Viper the file name and paths to search, and autoloads the config. Returns an error if no config file is found.
Example ¶
viper.Reset()
SetFile("config", "./", "$HOME/.myproj", "/etc/myproj", "./testdata")
file := viper.ConfigFileUsed()
here, _ := filepath.Abs("./")
found, _ := filepath.Rel(here, file)
fmt.Println(found)
Output: testdata/config.yaml
func ShowConfig ¶ added in v0.4.0
func ShowConfig() bool
ShowConfig will output all config values as yaml if the --print-config flag is set returns the value of the print-config flag