/
feature.go
296 lines (275 loc) · 8.07 KB
/
feature.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
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"os"
"strings"
"github.com/cozy/cozy-stack/client/request"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/spf13/cobra"
)
var flagWithSources bool
var featureCmdGroup = &cobra.Command{
Use: "features <command>",
Aliases: []string{"feature"},
Short: "Manage the feature flags",
}
var featureShowCmd = &cobra.Command{
Use: "show",
Short: `Display the computed feature flags for an instance`,
Long: `
cozy-stack feature show displays the feature flags that are shown by apps.
`,
Example: `$ cozy-stack feature show --domain cozy.localhost:8080`,
RunE: func(cmd *cobra.Command, args []string) error {
if flagDomain == "" {
errPrintfln("%s", errMissingDomain)
return cmd.Usage()
}
c := newClient(flagDomain, consts.Settings)
req := &request.Options{
Method: "GET",
Path: "/settings/flags",
}
if flagWithSources {
req.Queries = url.Values{"include": {"source"}}
}
res, err := c.Req(req)
if err != nil {
return err
}
defer res.Body.Close()
var obj struct {
Data struct {
Attributes map[string]json.RawMessage `json:"attributes"`
} `json:"data"`
Included []struct {
ID string `json:"id"`
Attributes map[string]json.RawMessage `json:"attributes"`
} `json:"included"`
}
if err = json.NewDecoder(res.Body).Decode(&obj); err != nil {
return err
}
for k, v := range obj.Data.Attributes {
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
if len(obj.Included) > 0 {
fmt.Fprintf(os.Stdout, "\nSources:\n")
for _, source := range obj.Included {
fmt.Fprintf(os.Stdout, "- %s\n", source.ID)
for k, v := range source.Attributes {
fmt.Fprintf(os.Stdout, "\t- %s: %s\n", k, string(v))
}
}
}
return nil
},
}
var featureFlagCmd = &cobra.Command{
Use: "flags",
Aliases: []string{"flag"},
Short: `Display and update the feature flags for an instance`,
Long: `
cozy-stack feature flags displays the feature flags that are specific to an instance.
It can also take a list of flags to update.
If you give a null value, the flag will be removed.
`,
Example: `$ cozy-stack feature flags --domain cozy.localhost:8080 '{"add_this_flag": true, "remove_this_flag": null}'`,
RunE: func(cmd *cobra.Command, args []string) error {
if flagDomain == "" {
errPrintfln("%s", errMissingDomain)
return cmd.Usage()
}
ac := newAdminClient()
req := request.Options{
Method: "GET",
Path: fmt.Sprintf("/instances/%s/feature/flags", flagDomain),
}
if len(args) > 0 {
req.Method = "PATCH"
req.Body = strings.NewReader(args[0])
}
res, err := ac.Req(&req)
if err != nil {
return err
}
defer res.Body.Close()
var obj map[string]json.RawMessage
if err = json.NewDecoder(res.Body).Decode(&obj); err != nil {
return err
}
for k, v := range obj {
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
}
var featureSetCmd = &cobra.Command{
Use: "sets",
Short: `Display and update the feature sets for an instance`,
Long: `
cozy-stack feature sets displays the feature sets coming from the manager.
It can also take a space-separated list of sets that will replace the previous
list (no merge).
All the sets can be removed by setting an empty list ('').
`,
Example: `$ cozy-stack feature sets --domain cozy.localhost:8080 'set1 set2'`,
RunE: func(cmd *cobra.Command, args []string) error {
if flagDomain == "" {
errPrintfln("%s", errMissingDomain)
return cmd.Usage()
}
ac := newAdminClient()
req := request.Options{
Method: "GET",
Path: fmt.Sprintf("/instances/%s/feature/sets", flagDomain),
}
if len(args) > 0 {
list := args
if len(args) == 1 {
list = strings.Fields(args[0])
}
buf, err := json.Marshal(list)
if err != nil {
return err
}
req.Method = "PUT"
req.Body = bytes.NewReader(buf)
}
res, err := ac.Req(&req)
if err != nil {
return err
}
defer res.Body.Close()
var sets []string
if err = json.NewDecoder(res.Body).Decode(&sets); err != nil {
return err
}
for _, set := range sets {
fmt.Fprintf(os.Stdout, "- %v\n", set)
}
return nil
},
}
var featureRatioCmd = &cobra.Command{
Use: "ratio <context-name>",
Aliases: []string{"context"},
Short: `Display and update the feature flags for a context`,
Long: `
cozy-stack feature ratio displays the feature flags for a context.
It can also create, update, or remove flags (with a ratio and value).
To remove a flag, set it to an empty array (or null).
`,
Example: `$ cozy-stack feature ratio --context beta '{"set_this_flag": [{"ratio": 0.1, "value": 1}, {"ratio": 0.9, "value": 2}] }'`,
RunE: func(cmd *cobra.Command, args []string) error {
if flagContext == "" {
return cmd.Usage()
}
ac := newAdminClient()
req := request.Options{
Method: "GET",
Path: fmt.Sprintf("/instances/feature/contexts/%s", flagContext),
}
if len(args) > 0 {
req.Method = "PATCH"
req.Body = strings.NewReader(args[0])
}
res, err := ac.Req(&req)
if err != nil {
return err
}
defer res.Body.Close()
var obj map[string]json.RawMessage
if err = json.NewDecoder(res.Body).Decode(&obj); err != nil {
return err
}
for k, v := range obj {
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
}
var featureConfigCmd = &cobra.Command{
Use: "config <context-name>",
Short: `Display the feature flags from configuration for a context`,
Long: `
cozy-stack feature config displays the feature flags from configuration for a context.
These flags are read only and can only be updated by changing configuration and restarting the stack.
`,
Example: `$ cozy-stack feature config --context beta`,
RunE: func(cmd *cobra.Command, args []string) error {
if flagContext == "" {
return cmd.Usage()
}
ac := newAdminClient()
req := request.Options{
Method: "GET",
Path: fmt.Sprintf("/instances/feature/config/%s", flagContext),
}
res, err := ac.Req(&req)
if err != nil {
return err
}
defer res.Body.Close()
var obj map[string]json.RawMessage
if err = json.NewDecoder(res.Body).Decode(&obj); err != nil {
return err
}
for k, v := range obj {
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
}
var featureDefaultCmd = &cobra.Command{
Use: "defaults",
Short: `Display and update the default values for feature flags`,
Long: `
cozy-stack feature defaults displays the default values for feature flags.
It can also take a list of flags to update.
If you give a null value, the flag will be removed.
`,
Example: `$ cozy-stack feature defaults '{"add_this_flag": true, "remove_this_flag": null}'`,
RunE: func(cmd *cobra.Command, args []string) error {
ac := newAdminClient()
req := request.Options{
Method: "GET",
Path: "/instances/feature/defaults",
}
if len(args) > 0 {
req.Method = "PATCH"
req.Body = strings.NewReader(args[0])
}
res, err := ac.Req(&req)
if err != nil {
return err
}
defer res.Body.Close()
var obj map[string]json.RawMessage
if err = json.NewDecoder(res.Body).Decode(&obj); err != nil {
return err
}
for k, v := range obj {
fmt.Fprintf(os.Stdout, "- %s: %s\n", k, string(v))
}
return nil
},
}
func init() {
featureShowCmd.Flags().StringVar(&flagDomain, "domain", "", "Specify the domain name of the instance")
featureShowCmd.Flags().BoolVar(&flagWithSources, "source", false, "Show the sources of the feature flags")
featureFlagCmd.Flags().StringVar(&flagDomain, "domain", "", "Specify the domain name of the instance")
featureSetCmd.Flags().StringVar(&flagDomain, "domain", "", "Specify the domain name of the instance")
featureRatioCmd.Flags().StringVar(&flagContext, "context", "", "The context for the feature flags")
featureConfigCmd.Flags().StringVar(&flagContext, "context", "", "The context for the feature flags")
featureCmdGroup.AddCommand(featureShowCmd)
featureCmdGroup.AddCommand(featureFlagCmd)
featureCmdGroup.AddCommand(featureSetCmd)
featureCmdGroup.AddCommand(featureRatioCmd)
featureCmdGroup.AddCommand(featureConfigCmd)
featureCmdGroup.AddCommand(featureDefaultCmd)
RootCmd.AddCommand(featureCmdGroup)
}