cli tool to generate code for the application specific configuration.
$ go get -u github.com/suzuki-shunsuke/go-gencfg
or Download a binary from the release page.
Check whether gencfg is installed.
$ gencfg -v
gencfg version 0.1.0
- Generate configuration file.
$ gencfg init
create .gencfg.yml
create .gencfg_config.tmpl
- Edit configuration file according to the application.
Here is the example.
dest: config/config.go
package_name:
package:
template: .gencfg_config.tmpl
formatters:
- gofmt -l -s -w
default:
env:
bind: true
flag:
bind: false
params:
- name: user
type: string
- name: port
type: int
default: 6000
- name: access_token
type: string
- Generate code.
$ gencfg gen
- The end. You can manage configuration with generated code.
Use global configuration.
config.InitGlobalConfig()
config.GetUser()
config.SetUser("foo")
Or use local configuration.
cfg := config.New()
cfg.GetUser()
cfg.SetUser("foo")
When you update the configuration file .gencfg.yml
, please run gencfg gen
again.
- We can manage the application configuration items with YAML. New developers can understand what the application configuration items is by YAML.
- We don't have to write obvious code. We can focus on the application logic.
- We can access configuration with better interfaces. viper specifies the configuration item by string key. This may cause the typo of the key. The typo of key doesn't raise compile error and probably we can't find the typo even with linter. On the other hand, typo of method name raise the compile error so we can find the typo easily.
viper.GetString("user")
config.GetUser()
See USAGE.md .
gencfg compare -f
is useful to validate the consistency of configuration file (.gencfg.yml
) and the generated code.
gencfg compare -f
returns 0
if the result of gencfg gen
and existing code is equal and otherwise returns not 0
.
It is useful for CI.
See CONFIGURATION.md .