/
options.go
233 lines (198 loc) · 6.67 KB
/
options.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
package kubernetes
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
)
const (
BooleanType = iota
StringType
IntType
)
// OptionsReader is the interface to the structures and objects used
// to fill InstallationOption instances with a valid value.
//
// Note, each reader has the discretion to not modify the provided
// option instance based on its state. The option's Valid flag is, for
// example, how the defaults, cli, and interactive readers communicate
// and decide which options to handle.
type OptionsReader interface {
Read(*InstallationOption) error
}
// A InstallationOptionDynamicDefault function may provide a dynamic
// default value for an option. When present it has precedence over
// any static default value in the structure.
//
// ATTENTION: The function is responsible for setting both Value and
// Valid flag of the specified option. This is necessary for cases
// where the dynamic default could not be determined, yet is not an
// error.
//
type InstallationOptionDynamicDefault func(o *InstallationOption) error
type InstallationOptionType int
type InstallationOption struct {
Name string // Identifying name of the configuration variable
Value interface{} // Value to use (may not be valid, see `Valid` field).
Default interface{} // Static default value for the value.
DynDefaultFunc InstallationOptionDynamicDefault // Function to provide a default. Has priority over `Default`.
UserSpecified bool // Flag, true if `Value` came from the user.
Description string // Short description of the variable
Type InstallationOptionType // Type information for `Value` and `Default`.
DeploymentID string // If set, this option will be passed only to this deployment (private)
}
type InstallationOptions []InstallationOption
func (opts InstallationOptions) AsCobraFlagsFor(cmd *cobra.Command) {
for _, opt := range opts {
// Translate option name
flagName := strings.ReplaceAll(opt.Name, "_", "-")
// Declare option's flag, type-dependent
switch opt.Type {
case BooleanType:
if opt.Default == nil {
cmd.Flags().Bool(flagName, false, opt.Description)
} else {
cmd.Flags().Bool(flagName, opt.Default.(bool), opt.Description)
}
case StringType:
if opt.Default == nil {
cmd.Flags().String(flagName, "", opt.Description)
} else {
cmd.Flags().String(flagName, opt.Default.(string), opt.Description)
}
case IntType:
if opt.Default == nil {
cmd.Flags().Int(flagName, 0, opt.Description)
} else {
cmd.Flags().Int(flagName, opt.Default.(int), opt.Description)
}
}
}
return
}
func (opts InstallationOptions) ToOptMap() map[string]InstallationOption {
result := map[string]InstallationOption{}
for _, opt := range opts {
result[opt.ToOptMapKey()] = opt
}
return result
}
func (opt InstallationOption) ToOptMapKey() string {
return fmt.Sprintf("%s-%s", opt.Name, opt.DeploymentID)
}
func (opt *InstallationOption) DynDefault() error {
return opt.DynDefaultFunc(opt)
}
func (opt *InstallationOption) SetDefault() error {
// Give priority to a function which provides the default
// value dynamically.
if opt.DynDefaultFunc != nil {
err := opt.DynDefault()
if err != nil {
return err
}
} else if opt.Default != nil {
opt.Value = opt.Default
}
return nil
}
// Merge returns a merge of the two options respecting uniqueness of name+deploymentID
func (opts InstallationOptions) Merge(toMerge InstallationOptions) InstallationOptions {
result := InstallationOptions{}
optMap := opts.ToOptMap()
for _, mergeOpt := range toMerge {
optMap[mergeOpt.ToOptMapKey()] = mergeOpt
}
for _, v := range optMap {
result = append(result, v)
}
return result
}
// GetOpt finds the given option in opts.
//
// When the deploymentID is the empty string the function searches for
// and returns only global options (not associated to any deployment).
// Otherwise it searches for private options associated with the
// specified deployment as well.
//
// ATTENTION: In the second case private options have precedence. In
// other words if we have private and global options of the same name,
// then the private option is returned.
//
// ATTENTION: This function returns a reference, enabling the caller
// to modify the structure.
func (opts InstallationOptions) GetOpt(optionName string, deploymentID string) (*InstallationOption, error) {
if deploymentID != "" {
// "Private" options first, only if a deployment to search is known
for i, option := range opts {
if option.Name == optionName && option.DeploymentID == deploymentID {
return &opts[i], nil
}
}
}
// If there is no private option, try "Global" options
for i, option := range opts {
if option.Name == optionName && option.DeploymentID == "" {
return &opts[i], nil
}
}
return nil, errors.New(optionName + " not set")
}
func (opts InstallationOptions) GetString(optionName string, deploymentID string) (string, error) {
option, err := opts.GetOpt(optionName, deploymentID)
if err != nil {
return "", err
}
result, ok := option.Value.(string)
if !ok {
panic("wrong type assertion")
}
return result, nil
}
func (opts InstallationOptions) GetBool(optionName string, deploymentID string) (bool, error) {
option, err := opts.GetOpt(optionName, deploymentID)
if err != nil {
return false, err
}
result, ok := option.Value.(bool)
if !ok {
panic("wrong type assertion")
}
return result, nil
}
func (opts InstallationOptions) GetInt(optionName string, deploymentID string) (int, error) {
option, err := opts.GetOpt(optionName, deploymentID)
if err != nil {
return 0, err
}
result, ok := option.Value.(int)
if !ok {
panic("wrong type assertion")
}
return result, nil
}
func (opts InstallationOptions) ForDeployment(deploymentID string) InstallationOptions {
result := InstallationOptions{}
for _, opt := range opts {
if opt.DeploymentID == deploymentID || opt.DeploymentID == "" {
result = append(result, opt)
}
}
return result
}
// Populate will try to give values to the needed options
// using the given OptionsReader. If none is given, the default is the
// InteractiveOptionsReader which will ask in the terminal.
// This method only populates what is possible and leaves the rest empty.
// TODO: Implement another method to validate that all options have been set.
func (opts *InstallationOptions) Populate(reader OptionsReader) (*InstallationOptions, error) {
newOpts := InstallationOptions{}
for _, opt := range *opts {
err := reader.Read(&opt)
if err != nil {
return opts, err
}
newOpts = append(newOpts, opt)
}
return &newOpts, nil
}