-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd_config_modify.go
126 lines (97 loc) · 3.49 KB
/
cmd_config_modify.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
package main
import (
"fmt"
"github.com/bobziuchkovski/writ"
"github.com/jkomoros/boardgame/boardgame-util/lib/config"
)
//configModify isn't used directly but is a super-class for all Config
//commands that modify the config.
type configModify struct {
baseSubCommand
Secret bool
Dev bool
Prod bool
Force bool
}
//If fieldType is one this responds to, should either teturn an updater or
//errAndQuit. Otherwise, OK to return nil to signal it's not a valid tyep.
//fieldType won't be TypeInvalid; that will already be screened out.
type updateFactory func(base *boardgameUtil, field config.ModeField, fieldType config.ModeFieldType, positional []string) config.Updater
func (c *configModify) RunWithUpdateFactory(p writ.Path, positional []string, factory updateFactory) {
cfg := c.Base().GetConfig(c.Force)
mode := deriveMode(c.Dev, c.Prod)
if len(positional) < 1 {
c.Base().errAndQuit("KEY must be provided")
}
field := config.FieldFromString(positional[0])
fieldType := config.FieldTypes[field]
if fieldType == config.FieldTypeInvalid {
c.Base().errAndQuit(positional[0] + " is not a valid field")
}
if !c.ConfirmField(field) {
c.Base().errAndQuit("Didn't confirm secret field set.")
}
updater := factory(c.Base(), field, fieldType, positional)
if updater == nil {
c.Base().errAndQuit("Invalid field type for this command")
}
if err := cfg.Update(mode, c.Secret, updater); err != nil {
c.Base().errAndQuit("Couldn't update value: " + err.Error())
}
if err := cfg.Save(); err != nil {
c.Base().errAndQuit("Couldn't save updated config files: " + err.Error())
}
}
func (c *configModify) ConfirmField(field config.ModeField) bool {
//We only warn for secret fields on prod.
if !c.Prod {
if field == config.FieldOfflineDevMode {
fmt.Println("WARNING: Typically you don't set offlinedevmode in your config, but pass --offline-dev-mode to 'boardgame-util serve' when you want it.")
}
return true
}
//Setting on secret is always fine.
if c.Secret {
return true
}
sensitiveFields := map[config.ModeField]bool{
config.FieldAdminUserIds: true,
config.FieldStorage: true,
}
//Only show confirm for sensitive fields.
if !sensitiveFields[field] {
return true
}
return baseConfirm("You have proposed setting a field that is typically secret on prod without passing `--secret`, which means the configuration would be added to the public config that might be checked into source control.")
}
func (c *configModify) WritOptions() []*writ.Option {
return []*writ.Option{
{
Names: []string{"s", "secret"},
Flag: true,
Description: "If provided, will set the secret config instead of public config, creating it if necessary.",
Decoder: writ.NewFlagDecoder(&c.Secret),
},
{
Names: []string{"d", "dev"},
Description: "If set, will write to dev options instead of base. No effect if prod is also passed",
Flag: true,
Decoder: writ.NewFlagDecoder(&c.Dev),
},
{
Names: []string{"p", "prod"},
Description: "If set, will write to prod options instead of base. No effect if dev is also passed",
Flag: true,
Decoder: writ.NewFlagDecoder(&c.Prod),
},
{
Names: []string{"f", "force"},
Description: "If provided, will allow modifying the given property even if the underlying config does not yet exist.",
Flag: true,
Decoder: writ.NewFlagDecoder(&c.Force),
},
}
}
func (c *configModify) Usage() string {
return "[--secret] [--dev|--prod] KEY [SUB-KEY] VAL"
}