go-gencfg

command module
v2.0.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2018 License: MIT Imports: 5 Imported by: 0

README

go-gencfg

GoDoc CircleCI codecov Go Report Card GitHub last commit GitHub tag License

cli tool to generate code for the application specific configuration.

Install

$ 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

Getting Started

  1. Generate configuration file.
$ gencfg init
create .gencfg.yml
create .gencfg_config.tmpl
  1. 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
  1. Generate code.
$ gencfg gen
  1. 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.

Why we use this tool?

  1. We can manage the application configuration items with YAML. New developers can understand what the application configuration items is by YAML.
  2. We don't have to write obvious code. We can focus on the application logic.
  3. 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()

Usage

See USAGE.md .

validate for ci

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.

Configuration

See CONFIGURATION.md .

License

MIT

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package command provides sub command's entrypoints.
Package command provides sub command's entrypoints.
Package config treats application's configuration.
Package config treats application's configuration.
Package domain is the domain layer.
Package domain is the domain layer.
Package infra mainly treats side effects.
Package infra mainly treats side effects.
Package registry converts implementations to interface and reduces each layer's dependencies.
Package registry converts implementations to interface and reduces each layer's dependencies.
Package test is test double.
Package test is test double.
Package usecase provides application logic.
Package usecase provides application logic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL