-
Notifications
You must be signed in to change notification settings - Fork 1
/
king.go
230 lines (181 loc) · 4.18 KB
/
king.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Package king is a library to configure the command line parser
// https://github.com/alecthomas/kong
package king
import (
"context"
"regexp"
"sort"
"strings"
"github.com/alecthomas/kong"
)
const redactChar = `*`
// Config is used to create DefaultOptions.
type Config struct {
Context context.Context
Name string
Description string
BuildInfo *BuildInfo
ConfigPaths []string
Variables map[string]string
FileResolver FileResolver
}
func (c Config) pathString() string {
return strings.Join(c.ConfigPaths, ",")
}
// 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)
}
vars := kong.Vars{
configPathsKey: c.pathString(),
}
for k, v := range c.Variables {
vars[k] = v
}
if c.BuildInfo != nil {
vars[versionKey] = c.BuildInfo.Version(c.Name).String()
for k, v := range c.BuildInfo.asMap("king_") {
vars[k] = v
}
}
opts := []kong.Option{
kong.Name(c.Name),
kong.Description(c.Description),
kong.ValueFormatter(newHelpFormatter(c.Name)),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
kong.UsageOnError(),
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...), kong.Resolvers(EnvResolver()))
}
return opts
}
// Map is a map with string as key and interface{} as value.
type Map map[string]interface{}
// FlagMap returns the flags and corresponding values from *kong.Context.
//
// To prevent logging sensitive flag values it is possible to provide
// a list of regular expressions. Flag values of flag names that match are
// redacted by '*'.
func FlagMap(ctx *kong.Context, redactFlags ...*regexp.Regexp) Map {
m := Map{}
for _, f := range ctx.Flags() {
m[f.Name] = ctx.FlagValue(f)
}
m = m.redact(redactFlags...)
b := newBuildInfo("king_", ctx.Model.Vars())
if b != nil {
m[buildInfoKey] = b
for k, v := range b.asMap("") {
m[k] = v
}
}
return m
}
func (m Map) redact(keyRegexp ...*regexp.Regexp) Map {
r := redactor(keyRegexp)
nm := Map{}
for k, v := range m {
nm[k] = r(k, v)
}
return nm
}
// Add adds key and values.
func (m Map) Add(keyVals ...string) Map {
nm := Map{}
for k, v := range m {
nm[k] = v
}
max := len(keyVals)
if max%2 != 0 {
max--
}
for i := 0; i < max; i += 2 {
nm[keyVals[i]] = keyVals[i+1]
}
return nm
}
// Rm removes keys from Map.
func (m Map) Rm(keys ...string) Map {
nm := Map{}
for k, v := range m {
if contains(keys, k) {
continue
}
nm[k] = v
}
return nm
}
// List returns the flag and values as list (sorted by keys).
func (m Map) List() []interface{} {
l := make([]interface{}, 0, 2*len(m))
for _, k := range m.keys() {
l = append(l, k, m[k])
}
return l
}
func (m Map) keys() []string {
keys := make([]string, 0, len(m))
for k := range m {
if k == buildInfoKey {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func bindContext(ctx context.Context) kong.Option {
return kong.BindTo(ctx, (*context.Context)(nil))
}
func redactor(targets []*regexp.Regexp) func(string, interface{}) interface{} {
return func(key string, value interface{}) interface{} {
s, ok := value.(string)
if !ok {
return value
}
for _, t := range targets {
if t.MatchString(strings.ToLower(key)) {
return strings.Repeat(string(redactChar), len(s))
}
}
return value
}
}
func contains(list []string, item string) bool {
for _, itm := range list {
if itm == item {
return true
}
}
return false
}
func newHelpFormatter(appName string) func(*kong.Value) string {
return func(value *kong.Value) string {
var suffix string
if len(value.Tag.Envs) == 0 {
envName := toEnvVarName(appName, value)
suffix = "($" + envName + ")"
} else {
suffix = "($" + value.Tag.Envs[0] + ")"
}
switch {
case strings.HasSuffix(value.Help, "."):
return value.Help[:len(value.Help)-1] + " " + suffix + "."
case value.Help == "":
return suffix
default:
return value.Help + " " + suffix
}
}
}