Multiconfig
Load configuration from multiple sources. Multiconfig makes loading/parsing
from different configuration sources an easy task. The problem with any app is
that with time there are many options how to populate a set of configs.
Multiconfig makes it easy by dynamically creating all necessary options.
Checkout the example below to see it in action.
Features
Multiconfig is able to read configuration automatically based on the given struct's field names from the following sources:
- Struct tags
- TOML file
- JSON file
- YAML file
- Environment variables
- Flags
Install
go get github.com/koding/multiconfig
Usage and Examples
Lets define and struct that defines our configuration
type Server struct {
Name string `required:"true"`
Port int `default:"6060"`
Enabled bool
Users []string
}
Load the configuration into multiconfig:
m := multiconfig.New()
m := multiconfig.NewWithPath("config.toml")
serverConf := new(Server)
err := m.Load(serverConf)
m.MustLoad(serverConf)
serverConf.Port
serverConf.Name
Run your app:
$ app
$ SERVER_PORT=4000 SERVER_NAME="koding" app
$ app -port 4000 -users "gopher,koding"
$ app -help
Usage of app:
-enabled=true: Change value of Enabled.
-name=Koding: Change value of Name.
-port=6060: Change value of Port.
-users=[ankara istanbul]: Change value of Users.
Generated environment variables:
SERVER_NAME
SERVER_PORT
SERVER_ENABLED
SERVER_USERS
License
The MIT License (MIT) - see LICENSE for more details