-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.go
More file actions
280 lines (245 loc) · 8.23 KB
/
Copy pathfeatures.go
File metadata and controls
280 lines (245 loc) · 8.23 KB
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
package cmd
import (
"fmt"
"strings"
"github.com/cego/gitte/config"
"github.com/cego/gitte/features"
"github.com/cego/gitte/output"
"github.com/cego/gitte/state"
"github.com/spf13/cobra"
)
func newFeaturesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "features",
Short: "Manage feature gates",
RunE: func(cmd *cobra.Command, args []string) error {
if outputMode() == output.ModePlain {
return newFeaturesListCmd().RunE(cmd, args)
}
err := features.Run(globalCfg, globalCwd, globalSt)
if err == features.ErrNoFeatureGates {
fmt.Println(err)
return nil
}
return err
},
}
cmd.AddCommand(
newFeaturesListCmd(),
newFeaturesEnableCmd(),
newFeaturesDisableCmd(),
)
return cmd
}
func newFeaturesListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List all feature gates and their state",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("%-30s %-8s SCOPE\n", "GATE", "ENABLED")
fmt.Printf("%-30s %-8s -----\n", "----", "-------")
for gateName, gate := range globalCfg.FeatureGates {
enabled := "no"
scopeDesc := buildScopeDescription(gate.Scope.Projects, gate.Scope.GitlabGroups, gate.Scope.GithubOrgs)
if fs, ok := globalSt.Features[gateName]; ok && fs.Enabled {
enabled = "yes"
if fs.OverrideScope != nil {
scopeDesc = buildOverrideScopeDescription(fs.OverrideScope)
}
}
fmt.Printf("%-30s %-8s %s\n", gateName, enabled, scopeDesc)
}
return nil
},
}
}
func newFeaturesEnableCmd() *cobra.Command {
var (
projects []string
gitlabGroups []string
githubOrgs []string
excludes []string
)
cmd := &cobra.Command{
Use: "enable <gate>",
Short: "Enable a feature gate",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
gateName := args[0]
if _, ok := globalCfg.FeatureGates[gateName]; !ok {
return fmt.Errorf("unknown feature gate: %q", gateName)
}
if len(excludes) > 0 && len(gitlabGroups) == 0 && len(githubOrgs) == 0 {
return fmt.Errorf("--exclude requires --gitlab-group or --github-org")
}
fs := state.FeatureState{Enabled: true}
if len(projects) > 0 || len(gitlabGroups) > 0 || len(githubOrgs) > 0 {
override, err := buildOverrideFromFlags(projects, gitlabGroups, githubOrgs, excludes)
if err != nil {
return err
}
fs.OverrideScope = override
}
globalSt.Features[gateName] = fs
if err := state.Save(globalCwd, globalSt); err != nil {
return fmt.Errorf("failed to save state: %w", err)
}
fmt.Printf("Feature gate %q enabled\n", gateName)
return nil
},
}
cmd.Flags().StringArrayVar(&projects, "project", nil, "limit to specific project(s)")
cmd.Flags().StringArrayVar(&gitlabGroups, "gitlab-group", nil, "limit to gitlab group (host/group)")
cmd.Flags().StringArrayVar(&githubOrgs, "github-org", nil, "limit to github org (host/org)")
cmd.Flags().StringArrayVar(&excludes, "exclude", nil, "exclude project from all groups/orgs")
return cmd
}
func newFeaturesDisableCmd() *cobra.Command {
var (
projects []string
gitlabGroups []string
githubOrgs []string
excludes []string
)
cmd := &cobra.Command{
Use: "disable <gate>",
Short: "Disable a feature gate, or (with scope flags) disable it only for specific projects",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
gateName := args[0]
if len(excludes) > 0 && len(gitlabGroups) == 0 && len(githubOrgs) == 0 {
return fmt.Errorf("--exclude requires --gitlab-group or --github-org")
}
fs, ok := globalSt.Features[gateName]
scoped := len(projects) > 0 || len(gitlabGroups) > 0 || len(githubOrgs) > 0
// Unscoped disable only consults state, so it can also clean up a stale
// entry for a gate that has since been removed from the config.
if !scoped {
if !ok {
fmt.Printf("Feature gate %q was not enabled\n", gateName)
return nil
}
delete(globalSt.Features, gateName)
if err := state.Save(globalCwd, globalSt); err != nil {
return fmt.Errorf("failed to save state: %w", err)
}
fmt.Printf("Feature gate %q disabled\n", gateName)
return nil
}
// Scoped disable needs the gate's configured scope to reconstruct the override.
gate, cfgOK := globalCfg.FeatureGates[gateName]
if !cfgOK {
return fmt.Errorf("unknown feature gate: %q", gateName)
}
if !ok || !fs.Enabled {
fmt.Printf("Feature gate %q was not enabled\n", gateName)
return nil
}
// Remove the matching projects from the gate's current scope, leaving it
// enabled for the rest.
removal, err := buildOverrideFromFlags(projects, gitlabGroups, githubOrgs, excludes)
if err != nil {
return err
}
scopeProjects := features.ProjectsInGateScope(globalCfg, gate)
checked := features.OverrideToCheckedState(fs.OverrideScope, scopeProjects)
removed := 0
for name, sp := range scopeProjects {
if checked[name] && features.ProjectMatchesOverrideScope(name, sp.Host, sp.Path, removal) {
checked[name] = false
removed++
}
}
if removed == 0 {
fmt.Printf("Feature gate %q was not enabled for the given project(s)\n", gateName)
return nil
}
anyLeft := false
for _, v := range checked {
if v {
anyLeft = true
break
}
}
if anyLeft {
fs.OverrideScope = features.CheckedStateToOverride(checked, scopeProjects)
globalSt.Features[gateName] = fs
} else {
delete(globalSt.Features, gateName)
}
if err := state.Save(globalCwd, globalSt); err != nil {
return fmt.Errorf("failed to save state: %w", err)
}
if anyLeft {
fmt.Printf("Feature gate %q disabled for the given project(s)\n", gateName)
} else {
fmt.Printf("Feature gate %q disabled\n", gateName)
}
return nil
},
}
cmd.Flags().StringArrayVar(&projects, "project", nil, "disable only for specific project(s)")
cmd.Flags().StringArrayVar(&gitlabGroups, "gitlab-group", nil, "disable only for gitlab group (host/group)")
cmd.Flags().StringArrayVar(&githubOrgs, "github-org", nil, "disable only for github org (host/org)")
cmd.Flags().StringArrayVar(&excludes, "exclude", nil, "exclude project from all groups/orgs")
return cmd
}
// buildOverrideFromFlags turns the CLI scope flags into a ScopeOverride.
func buildOverrideFromFlags(projects, gitlabGroups, githubOrgs, excludes []string) (*state.ScopeOverride, error) {
override := &state.ScopeOverride{Projects: projects}
for _, g := range gitlabGroups {
host, group, ok := strings.Cut(g, "/")
if !ok {
return nil, fmt.Errorf("invalid --gitlab-group format %q, expected host/group", g)
}
override.GitlabGroups = append(override.GitlabGroups, state.ScopeOverrideGroup{Host: host, Group: group, ExcludeProjects: excludes})
}
for _, o := range githubOrgs {
host, org, ok := strings.Cut(o, "/")
if !ok {
return nil, fmt.Errorf("invalid --github-org format %q, expected host/org", o)
}
override.GithubOrgs = append(override.GithubOrgs, state.ScopeOverrideOrg{Host: host, Org: org, ExcludeProjects: excludes})
}
return override, nil
}
func buildOverrideScopeDescription(o *state.ScopeOverride) string {
var parts []string
if len(o.Projects) > 0 {
parts = append(parts, "projects: "+strings.Join(o.Projects, ", "))
}
for _, g := range o.GitlabGroups {
s := fmt.Sprintf("gitlab:%s/%s", g.Host, g.Group)
if len(g.ExcludeProjects) > 0 {
s += " (excl: " + strings.Join(g.ExcludeProjects, ", ") + ")"
}
parts = append(parts, s)
}
for _, g := range o.GithubOrgs {
s := fmt.Sprintf("github:%s/%s", g.Host, g.Org)
if len(g.ExcludeProjects) > 0 {
s += " (excl: " + strings.Join(g.ExcludeProjects, ", ") + ")"
}
parts = append(parts, s)
}
if len(parts) == 0 {
return "none (disabled)"
}
return strings.Join(parts, "; ")
}
func buildScopeDescription(projects []string, gitlabGroups []config.GitlabScope, githubOrgs []config.GithubScope) string {
var parts []string
if len(projects) > 0 {
parts = append(parts, "projects: "+strings.Join(projects, ", "))
}
for _, g := range gitlabGroups {
parts = append(parts, fmt.Sprintf("gitlab:%s/%s", g.Host, g.Group))
}
for _, g := range githubOrgs {
parts = append(parts, fmt.Sprintf("github:%s/%s", g.Host, g.Org))
}
if len(parts) == 0 {
return "all projects"
}
return strings.Join(parts, "; ")
}