-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathconfig.go
More file actions
107 lines (94 loc) · 2.52 KB
/
config.go
File metadata and controls
107 lines (94 loc) · 2.52 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package config
import (
"bytes"
"errors"
"fmt"
"io"
flags "github.com/jessevdk/go-flags"
"github.com/spf13/viper"
)
type cmdArg struct {
fullName string
*flags.Option
}
func (a cmdArg) HasChanged() bool {
return a.IsSet() && !a.IsSetDefault()
}
func (a cmdArg) Name() string {
return a.fullName
}
func (a cmdArg) ValueString() string {
return fmt.Sprintf("%v", a.Value())
}
func (a cmdArg) ValueType() string {
return a.Field().Type.Name()
}
type cmdArgSet struct {
*flags.Parser
}
func eachGroup(g *flags.Group, f func(*flags.Group)) {
f(g)
for _, gg := range g.Groups() {
eachGroup(gg, f)
}
}
func eachOption(g *flags.Group, f func(*flags.Group, *flags.Option)) {
eachGroup(g, func(g *flags.Group) {
for _, option := range g.Options() {
f(g, option)
}
})
}
// VisitAll will execute fn() for all options found in command line.
// Since we have only two level of nesting it's enough to use simplified group-prefixed name.
func (cmdSet cmdArgSet) VisitAll(fn func(viper.FlagValue)) {
root := cmdSet.Group.Find("Application Options")
eachOption(root, func(g *flags.Group, o *flags.Option) {
name := o.LongName
if g != root {
name = g.ShortDescription + cmdSet.NamespaceDelimiter + name
}
fn(cmdArg{name, o})
})
}
func (cmdSet cmdArgSet) setDefaults(v *viper.Viper) {
eachOption(cmdSet.Group, func(g *flags.Group, o *flags.Option) {
if o.Default != nil && o.IsSetDefault() {
name := o.LongName
if g != cmdSet.Group {
name = g.ShortDescription + cmdSet.NamespaceDelimiter + name
}
v.SetDefault(name, o.Value())
}
})
}
// NewConfig returns a new instance of CmdOptions
func NewConfig(writer io.Writer) (*CmdOptions, error) {
v := viper.New()
p, err := Parse(writer)
if err != nil {
return nil, err
}
flagSet := cmdArgSet{p}
if err = v.BindFlagValues(flagSet); err != nil {
return nil, fmt.Errorf("cannot bind command-line flag values with viper: %w", err)
}
flagSet.setDefaults(v)
if v.IsSet("config") {
v.SetConfigFile(v.GetString("config"))
err := v.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return nil, fmt.Errorf("fatal error reading config file: %w", err)
}
}
conf := &CmdOptions{}
if err = v.Unmarshal(conf); err != nil {
return nil, fmt.Errorf("fatal error unmarshalling config file: %w", err)
}
if conf.ClientName == "" {
buf := bytes.NewBufferString("The required flag `-c, --clientname` was not specified\n")
p.WriteHelp(buf)
return conf, errors.New(buf.String())
}
return conf, nil
}