Skip to content

Commit

Permalink
feat: add toml config support
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren committed Jul 13, 2021
1 parent e66e146 commit e0f13a9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 34 deletions.
72 changes: 72 additions & 0 deletions file_resolvers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package king

import (
"io"
"io/ioutil"

"github.com/BurntSushi/toml"
"github.com/alecthomas/kong"
"gopkg.in/yaml.v3"
)

// FileResolver represents a kong fileresolver.
type FileResolver string

// All supported file resolvers.
const (
YAML FileResolver = "yaml"
TOML FileResolver = "toml"
)

// NewFileResolver creates a new fileresolver.
func NewFileResolver(f FileResolver) kong.ConfigurationLoader {
switch f {
case TOML:
return tomlResolver
default:
return yamlResolver
}
}

func yamlResolver(r io.Reader) (kong.Resolver, error) {
values := map[string]interface{}{}

err := yaml.NewDecoder(r).Decode(&values)
if err != nil {
return nil, err
}

var f kong.ResolverFunc = func(ctx *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
raw, ok := values[flag.Name]
if !ok {
return nil, nil
}

return raw, nil
}

return f, nil
}

func tomlResolver(r io.Reader) (kong.Resolver, error) {
values := map[string]interface{}{}

data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

if err := toml.Unmarshal(data, &values); err != nil {
return nil, err
}

var f kong.ResolverFunc = func(ctx *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) {
raw, ok := values[flag.Name]
if !ok {
return nil, nil
}
return raw, nil
}

return f, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/zbindenren/king
go 1.15

require (
github.com/BurntSushi/toml v0.3.1
github.com/alecthomas/kong v0.2.17
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/kong v0.2.17 h1:URDISCI96MIgcIlQyoCAlhOmrSw6pZScBNkctg8r0W0=
github.com/alecthomas/kong v0.2.17/go.mod h1:ka3VZ8GZNPXv9Ov+j4YNLkI8mTuhXyr/0ktSlqIydQQ=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
20 changes: 14 additions & 6 deletions king.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const redactChar = `*`

// Config is used to create DefaultOptions.
type Config struct {
Context context.Context
Name string
Description string
BuildInfo *BuildInfo
ConfigPaths []string
Context context.Context
Name string
Description string
BuildInfo *BuildInfo
ConfigPaths []string
FileResolver FileResolver
}

func (c Config) pathString() string {
Expand All @@ -28,6 +29,10 @@ func (c Config) pathString() string {

// DefaultOptions creates a set of opinionated options.
func DefaultOptions(c Config) []kong.Option {
if c.FileResolver == "" {
c.FileResolver = YAML
}

if c.ConfigPaths == nil {
c.ConfigPaths = configsForApp(c.Name)
}
Expand All @@ -53,14 +58,17 @@ func DefaultOptions(c Config) []kong.Option {
}),
kong.UsageOnError(),
kong.Resolvers(EnvResolver()),
kong.Configuration(YAML, c.ConfigPaths...),
vars,
}

if c.Context != nil {
opts = append(opts, bindContext(c.Context))
}

if len(c.ConfigPaths) > 0 {
opts = append(opts, kong.Configuration(NewFileResolver(c.FileResolver), c.ConfigPaths...))
}

return opts
}

Expand Down
31 changes: 31 additions & 0 deletions king_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,37 @@ override-config: "%s"
assert.Equal(t, expected, c)
}

func TestTOMLResolver(t *testing.T) {
cfgValue := "fromConfig"
path, cleanUpFile := writeFile(t, []byte(fmt.Sprintf(`from-config="%s"
override-config="%s"
`, cfgValue, cfgValue),
))
defer cleanUpFile()

expected := cli{
FromConfig: cfgValue,
OverrideConfig: cfgValue,
}

cli := cli{}
buf := &strings.Builder{}
opts := king.DefaultOptions(
king.Config{
Name: "test",
Description: "A application to test.",
ConfigPaths: []string{path},
FileResolver: king.TOML,
},
)
opts = append(opts, kong.Writers(buf, buf))
parser, err := kong.New(&cli, opts...)
require.NoError(t, err)
_, err = parser.Parse([]string{})
require.NoError(t, err)
assert.Equal(t, expected, cli)
}

func TestVersion(t *testing.T) {
buf := &strings.Builder{}
b, err := king.NewBuildInfo("1.0.0",
Expand Down
28 changes: 0 additions & 28 deletions yaml.go

This file was deleted.

0 comments on commit e0f13a9

Please sign in to comment.