-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd_config_set.go
151 lines (112 loc) · 4.01 KB
/
cmd_config_set.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
package main
import (
"errors"
"strings"
"github.com/bobziuchkovski/writ"
"github.com/jkomoros/boardgame/boardgame-util/lib/config"
)
type configSet struct {
configModify
}
func strToBool(in string) (bool, error) {
in = strings.ToLower(in)
if in == "0" || in == "false" {
return false, nil
}
if in == "1" || in == "true" {
return true, nil
}
return false, errors.New("Invalid bool string: " + in)
}
func deriveMode(dev, prod bool) config.ModeType {
//If one of dev or prod is set, return that
if dev || prod {
if dev {
return config.TypeDev
}
if prod {
return config.TypeProd
}
}
//Default to base
return config.TypeBase
}
func configSetFactory(base *boardgameUtil, field config.ModeField, fieldType config.ModeFieldType, positional []string) config.Updater {
switch fieldType {
case config.FieldTypeString:
if len(positional) != 2 {
base.errAndQuit("KEY of type string wants precisely one VAL")
}
return config.SetString(field, positional[1])
case config.FieldTypeStringSlice:
if len(positional) != 2 {
base.errAndQuit("KEY of type []string wants precisely one VAL")
}
return config.AddString(field, positional[1])
case config.FieldTypeStringMap:
if len(positional) != 3 {
base.errAndQuit("KEY of type map[string]string wants KEY SUB-KEY VAL")
}
return config.SetStringKey(field, positional[1], positional[2])
case config.FieldTypeBool:
if len(positional) != 2 {
base.errAndQuit("KEY of type bool wants one VAL")
}
b, err := strToBool(positional[1])
if err != nil {
base.errAndQuit(err.Error())
}
return config.SetBool(field, b)
case config.FieldTypeFirebase:
if len(positional) != 3 {
base.errAndQuit("KEY of type firebase wants KEY SUB-KEY VAL")
}
firebaseKey := config.FirebaseKeyFromString(positional[1])
if firebaseKey == config.FirebaseInvalid {
base.errAndQuit(positional[1] + " is not a valid firebase key")
}
return config.SetFirebaseKey(firebaseKey, positional[2])
case config.FieldTypeGameNode:
if len(positional) != 2 {
base.errAndQuit("games node wants precisely one VAL")
}
return config.AddGame(positional[1])
}
return nil
}
func (c *configSet) Run(p writ.Path, positional []string) {
c.configModify.RunWithUpdateFactory(p, positional, configSetFactory)
}
func (c *configSet) Name() string {
return "set"
}
func (c *configSet) Description() string {
return "Sets the given field to the given value"
}
func keyNamesForConfigType(typ config.ModeFieldType) []string {
var result []string
for key, val := range config.FieldTypes {
if val == typ {
result = append(result, "'"+string(key)+"'")
}
}
return result
}
func (c *configSet) HelpText() string {
var firebaseKeys []string
for key := range config.FirebaseKeys {
firebaseKeys = append(firebaseKeys, "'"+string(key)+"'")
}
return c.Name() + " sets the given field to the given value in the current config. KEY is not case sensitive.\n\n" +
"If KEY is of type string, simply sets the key to the given val. " +
"Keys of this type are (" + strings.Join(keyNamesForConfigType(config.FieldTypeString), ",") + ")\n\n" +
"If KEY is of type []string, simply adds the key to the given val if it doesn't exist. A simple convenience instead of `config add`. " +
"Keys of this type are (" + strings.Join(keyNamesForConfigType(config.FieldTypeStringSlice), ",") + "). `Games` is also of this type.\n\n" +
"If KEY is of type bool, val must be either '0', '1', 'true', 'false'. " +
"Keys of this type are (" + strings.Join(keyNamesForConfigType(config.FieldTypeBool), ",") + ")\n\n" +
"If KEY is of type map[key]val then SUB-KEY must also be provided. A value of '' will delete the key. " +
"Keys of this type are (" + strings.Join(keyNamesForConfigType(config.FieldTypeStringMap), ",") + "). " +
"'Firebase' is also of this type, but only allows the following sub-keys: (" +
strings.Join(firebaseKeys, ",") + "), and keys set to value of '' will not be deleted.\n\n" +
"See help for the parent command for more information about configuration in general."
}