Goconfig is an extremely simple and powerful configuration library for your Go programs that read values from environment vars, command line arguments and configuration file in JSON.
Make your configuration flags compact and easy to read.
Arguments are parsed from command line with the standard flag
library.
Define your structure with descriptions:
type myconfig struct {
Name string `usage:"The name of something"`
EnableLog bool `usage:"Enable logging into logdb" json:"enable_log"`
MaxProcs int `usage:"Maximum number of procs"`
UsersDB db
LogDB db
}
type db struct {
Host string `usage:"Host where db is located"`
User string `usage:"Database user"`
Pass string `usage:"Database password"`
}
Instance your config with default values:
c := &myconfig{
EnableLog: true,
UsersDB: db{
Host: "localhost",
User: "root",
Pass: "123456",
},
}
Fill your config:
goconfig.Read(c)
How the -help
looks like:
Usage of example:
-enablelog
Enable logging into logdb (default true)
-logdb.host string
Host where db is located (default "localhost")
-logdb.pass string
Database password (default "123456")
-logdb.user string
Database user (default "root")
-maxprocs int
Maximum number of procs
-name string
The name of something
-usersdb.host string
Host where db is located
-usersdb.pass string
Database password
-usersdb.user string
Database user
Mainly almost all types from flag
library are supported:
- bool
- float64
- int64
- int
- string
- uint64
- uint
- struct (hyerarchical keys)
- array (any type)
For the moment duration
type is not supported.
Since flag
library is using the key -help
to show usage, Goconf is behaving
in the same way.
Builtin flag -config
allow read configuration from a file. For the example
configuration above, this is a sample config.json file:
{
"name": "Fulanito",
"usersdb": {
"host": "localhost",
"user": "admin",
"pass": "123"
}
}
Configuration precedence is as follows (higher to lower):
- Arg command line
- Json config file
- Environment variable
- Default value
Feel free to fork, make changes and pull-request to master branch.
If you prefer, create a new issue or email me for new features, issues or whatever.
This command will pass all tests.
make
This project includes a sample project with a sample configuration. To make the binary:
make example