-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_config.go
416 lines (383 loc) · 9.34 KB
/
cmd_config.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
"golang.org/x/net/context"
)
type CmdConfigGet struct {
libkb.Contextified
Path string
Direct bool
Bare bool
AssertTrue bool
AssertFalse bool
AssertOkOnNil bool
}
type CmdConfigSet struct {
libkb.Contextified
Path string
Value keybase1.ConfigValue
DoClear bool
}
type CmdConfigInfo struct {
libkb.Contextified
}
func (v *CmdConfigGet) ParseArgv(ctx *cli.Context) error {
if ctx.Bool("direct") {
v.Direct = true
}
if ctx.Bool("bare") {
v.Bare = true
}
if ctx.Bool("assert-true") {
v.AssertTrue = true
}
if ctx.Bool("assert-false") {
v.AssertFalse = true
}
if v.AssertTrue && v.AssertFalse {
return fmt.Errorf("Cannot assert both true and false.")
}
if ctx.Bool("assert-ok-on-nil") {
v.AssertOkOnNil = true
}
if v.AssertOkOnNil && !(v.AssertTrue || v.AssertFalse) {
return fmt.Errorf("Must --assert-true or --assert-false to --assert-ok-on-nil.")
}
if (v.AssertTrue || v.AssertFalse) && !v.Direct {
return fmt.Errorf("Cannot --assert-true or --assert-false unless in --direct mode.")
}
if len(ctx.Args()) == 1 {
v.Path = ctx.Args()[0]
} else if len(ctx.Args()) > 1 {
return fmt.Errorf("Expected 0 or 1 arguments")
}
return nil
}
func (v *CmdConfigSet) ParseArgv(ctx *cli.Context) error {
flags := 0
args := ctx.Args()
if len(ctx.Args()) < 1 {
return fmt.Errorf("Need 1 or more arguments for set")
}
v.Path = args[0]
if ctx.Bool("clear") {
flags++
v.DoClear = true
}
if ctx.Bool("null") {
flags++
v.Value.IsNull = true
}
if ctx.Bool("int") {
if len(args) <= 1 {
return fmt.Errorf("Missing int value argument")
}
flags++
i, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}
tmp := int(i)
v.Value.I = &tmp
}
if ctx.Bool("float") {
if len(args) <= 1 {
return fmt.Errorf("Missing float value argument")
}
flags++
f, err := strconv.ParseFloat(args[1], 64)
if err != nil {
return err
}
v.Value.F = &f
}
if ctx.Bool("bool") {
if len(args) <= 1 {
return fmt.Errorf("Missing bool value argument")
}
flags++
b, err := strconv.ParseBool(args[1])
if err != nil {
return err
}
v.Value.B = &b
}
if ctx.Bool("obj") {
if len(args) <= 1 {
return fmt.Errorf("Missing obj value argument")
}
flags++
s := args[1]
v.Value.O = &s
}
if ctx.Bool("string") {
flags++
}
if flags > 1 {
return fmt.Errorf("Can only specify one of -c, -n, -i, -f, -b or -s")
}
if ctx.Bool("string") || flags == 0 {
if len(args) <= 1 {
return fmt.Errorf("Missing string value argument")
}
s := args[1]
if !ctx.IsSet("string") && v.looksLikeBool(s) {
return fmt.Errorf("The value %q looks like a boolean value, not a string. Use the -b flag to set a bool, or -s to confirm this is a string value.", s)
}
v.Value.S = &s
}
return nil
}
// like strconv.ParseBool, but without 0 and 1.
func (v *CmdConfigSet) looksLikeBool(s string) bool {
switch s {
case "t", "T", "true", "TRUE", "True":
return true
case "f", "F", "false", "FALSE", "False":
return true
}
return false
}
func (v *CmdConfigInfo) ParseArgv(ctx *cli.Context) error {
return nil
}
func (v *CmdConfigGet) runDirect(dui libkb.DumbOutputUI) error {
config := v.G().Env.GetConfig()
i, err := config.GetInterfaceAtPath(v.Path)
if err != nil {
if v.AssertOkOnNil {
_, isJSONError := err.(*jsonw.Error)
isJSONNoSuchKeyError := isJSONError && strings.Contains(err.Error(), "no such key")
// Don't print a warning if the error is that the directory/file
// doesn't exist or the key is not in the file. Otherwise, e.g., if
// the permissions are incorrect or the config file contains
// malformed JSON, print a warning but still don't return an error.
if !(os.IsNotExist(err) || isJSONNoSuchKeyError) {
v.G().Log.Warning(fmt.Sprintf("Unexpected error while reading config %s; ignoring.", err))
}
return nil
}
return err
}
if i == nil {
dui.Printf("null\n")
} else {
switch val := i.(type) {
case int:
dui.Printf("%d\n", val)
case string:
if v.Bare {
dui.Printf("%s\n", val)
} else {
dui.Printf("%q\n", val)
}
case bool:
dui.Printf("%t\n", val)
if v.AssertTrue && !val {
return fmt.Errorf("Assertion failed.")
}
if v.AssertFalse && val {
return fmt.Errorf("Assertion failed.")
}
case float64:
dui.Printf("%d\n", int(val))
default:
var b []byte
b, err = json.Marshal(val)
if err != nil {
return err
}
dui.Printf("%s\n", string(b))
}
if v.AssertTrue || v.AssertFalse {
if _, ok := i.(bool); !ok {
return fmt.Errorf("Not a boolean.")
}
}
}
return nil
}
func (v *CmdConfigGet) runClient(dui libkb.DumbOutputUI) error {
cli, err := GetConfigClient(v.G())
if err != nil {
return err
}
var val keybase1.ConfigValue
val, err = cli.GetValue(context.TODO(), v.Path)
if err != nil {
return err
}
switch {
case val.IsNull:
dui.Printf("null\n")
case val.I != nil:
dui.Printf("%d\n", *val.I)
case val.S != nil:
if v.Bare {
dui.Printf("%s\n", *val.S)
} else {
dui.Printf("%q\n", *val.S)
}
case val.B != nil:
dui.Printf("%t\n", *val.B)
case val.O != nil:
dui.Printf("%s\n", *val.O)
}
return nil
}
func (v *CmdConfigGet) Run() error {
dui := v.G().UI.GetDumbOutputUI()
if v.Direct {
return v.runDirect(dui)
}
return v.runClient(dui)
}
func (v *CmdConfigSet) Run() error {
cli, err := GetConfigClient(v.G())
if err != nil {
return err
}
if v.DoClear {
err = cli.ClearValue(context.TODO(), v.Path)
} else {
err = cli.SetValue(context.TODO(), keybase1.SetValueArg{Path: v.Path, Value: v.Value})
}
return err
}
func (v *CmdConfigInfo) Run() error {
configFile := v.G().Env.GetConfigFilename()
v.G().UI.GetDumbOutputUI().Printf("%s\n", configFile)
return nil
}
func NewCmdConfig(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "config",
ArgumentHelp: "[arguments...]",
Subcommands: []cli.Command{
NewCmdConfigGet(cl, g),
NewCmdConfigSet(cl, g),
NewCmdConfigInfo(cl, g),
},
}
}
func NewCmdConfigGetRunner(g *libkb.GlobalContext) *CmdConfigGet {
return &CmdConfigGet{Contextified: libkb.NewContextified(g)}
}
func NewCmdConfigGet(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "get",
Usage: "Get a config value",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "d, direct",
Usage: "Read the config value directly from the config file, without consulting the service",
},
cli.BoolFlag{
Name: "b, bare",
Usage: "Print string values without enclosing, JSON-style quotes",
},
cli.BoolFlag{
Name: "assert-true",
Usage: "Returns 0 exit code iff the value is a true boolean",
},
cli.BoolFlag{
Name: "assert-false",
Usage: "Returns 0 exit code iff the value is a false boolean",
},
cli.BoolFlag{
Name: "assert-ok-on-nil",
Usage: "Return 0 exit code if the value does not exist or the config file does not exist",
},
},
ArgumentHelp: "<key>",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdConfigGetRunner(g), "get", c)
if c.Bool("direct") {
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
}
},
}
}
func NewCmdConfigSet(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "set",
Usage: "Set a config value",
ArgumentHelp: "<key> <value>",
Description: "Set a config value. Specify an empty value to clear it.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "bool, b",
Usage: "Treat the passed argument as a boolean",
},
cli.BoolFlag{
Name: "int, i",
Usage: "Treat the passed argument as an integer",
},
cli.BoolFlag{
Name: "float, f",
Usage: "Treat the passed argument as a float",
},
cli.BoolFlag{
Name: "obj, o",
Usage: "Treat the passed argument as a JSON object",
},
cli.BoolFlag{
Name: "string, s",
Usage: "Treat the passed argument as a string (default)",
},
cli.BoolFlag{
Name: "null, n",
Usage: "Set the value to null",
},
cli.BoolFlag{
Name: "clear, c",
Usage: "Clear out the value",
},
},
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdConfigSetRunner(g), "set", c)
},
}
}
func NewCmdConfigSetRunner(g *libkb.GlobalContext) *CmdConfigSet {
return &CmdConfigSet{Contextified: libkb.NewContextified(g)}
}
func NewCmdConfigInfo(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "info",
Usage: "Show config file path",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdConfigInfo{Contextified: libkb.NewContextified(g)}, "info", c)
},
}
}
func (v *CmdConfigGet) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
// The root user may use the "config get -d" command to read
// config files.
AllowRoot: v.Direct,
}
}
func (v *CmdConfigSet) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
}
}
func (v *CmdConfigInfo) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
}
}