-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
62 lines (52 loc) · 1.98 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Package kzflag implements a koanf.Provider that reads commandline
// parameters as conf maps using zulucmd/zflag, a POSIX compliant
// alternative to Go's stdlib flag package.
package kzflag_test
import (
"fmt"
"log"
"os"
"testing"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
kzflag "github.com/zulucmd/koanf-zflag/v2"
"github.com/zulucmd/zflag/v2"
)
// Global koanf instance. Use "." as the key path delimiter. This can be "/" or any character.
var k = koanf.New(".")
func TestExample(t *testing.T) {
t.Parallel()
// Use the POSIX compliant zflag lib instead of Go's flag lib.
f := zflag.NewFlagSet("config", zflag.ContinueOnError)
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
// Path to one or more config files to load into koanf along with some config params.
f.StringSlice("conf", []string{"example/conf.json"}, "path to one or more .toml config files")
f.String("time", "2020-01-01", "a time string")
f.String("type", "xxx", "type of the app")
_ = f.Parse([]string{"--type", "hello world"})
// Load the config files provided in the commandline.
cFiles, _ := f.GetStringSlice("conf")
for _, c := range cFiles {
if err := k.Load(file.Provider(c), json.Parser()); err != nil {
log.Fatalf("error loading file: %v", err)
}
}
// "time" and "type" may have been loaded from the config file, but
// they can still be overridden with the values from the command line.
// The bundled kzflag.Provider takes a flagset from the gorwarden/zflag lib.
// Passing the Koanf instance to kzflag helps it deal with default command
// line flag values that are not present in conf maps from previously loaded
// providers.
if err := k.Load(kzflag.Provider(f, ".", kzflag.WithKoanf(k)), nil); err != nil {
log.Fatalf("error loading config: %v", err)
}
fmt.Println("time is = ", k.String("time"))
fmt.Println("type is = ", k.String("type"))
// Output:
// time is = 2019-01-01
// type is = hello world
}