Configo
Configo is a go library to parse toml configuration using struct tags
Features
- Configuring parser behaviour using struct tags
- Setting default value or as required
- Validating value using regex, range expression or named validator
- Generating toml template with human friendly information based on go struct and tags
- Building conf file generation tools using configo-build
QuickStart
- Install
configo-build
tool for config generation
go get github.com/distributedio/configo/bin/configo-build
- Define a struct in package conf
package conf
type Config struct {
Listen string `cfg:"listen; :8804; netaddr; The address the server to listen"`
MaxConn int `cfg:"max-connection; 10000; numeric; Max number of concurrent connections"`
Redis struct {
Cluster []string `cfg:"cluster; required; dialstring; The addresses of redis cluster"`
}
}
- Use
configo-build
tool generate config builder, if everything goes well, you'll get a binary called conf.Config.cfg
configo-build ./conf.Config
or use the absolute path
configo-build github.com/distributedio/configo/example/conf.Config
- execute builer to generate a toml
conf.config.cfg > conf.toml
or patch you toml file if it is already existed
conf.config.cfg -patch conf.toml
- Use your config in your code
import "github.com/distributedio/configo"
var conf conf.Config
if err := configo.Parse("{PATH_TO_YOUR_TOML}", &conf) ;err != nil {
}
Toml
shafreeck/toml is a modification version of naoina/toml,
adding the abililty to parse complex struct tags and with bugs fixed.
Validation
configo has a builtin validator with regex and range support
Supported named validator
- netaddr
- url
- nonempty
- dialstring
- boolean
- numeric
- printableascii
- path
and you can also use compare operator and regExp in tags
> 1
>=1
>1 <10
(1, )
(1, 10)
(1,10]
/[0-9]/
/[0-9]+/ (1, 10)
netaddr
numeric >10 ( ,100)
See the Suppoted Vaildator for all valid "named validators"
Struct tags
tags
has a key 'cfg' and its value consists of four parts: "name; default value or required; rule; descripion".
All four parts are splited by ";".
For example:
Listen `cfg:"listen; :8804; netaddr; The listen address of server"`
It looks like this when being marshaled to toml
You can see that we have rich information about the option. And the option is commented out too because it has a default value.
configo-build
Configo comes with a util tool called configo-build to build a configration file generator for you.
You can use the generator to generate your toml file or update it when you changed your source code(the configuration struct).
configo-build ./conf.Config
Generating your configuration file with the built generator
conf.config.cfg > conf.toml
conf.config.cfg -patch conf.toml