Repository navigation
Expand file tree
/
Copy pathfeature.go
More file actions
99 lines (82 loc) · 2.67 KB
/
Copy pathfeature.go
File metadata and controls
99 lines (82 loc) · 2.67 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
package laravel
import (
"cmp"
"context"
"flag"
"slices"
"strings"
"go.laravel.com/console"
"go.laravel.com/feature"
)
type featurePurgeCommand struct {
app *App
except []string
exceptDefined bool
}
func (*featurePurgeCommand) Name() string { return "feature:purge" }
func (*featurePurgeCommand) Description() string { return "Delete stored feature values" }
func (*featurePurgeCommand) Arguments() []Argument {
return []Argument{{Name: "FEATURES", Description: "The features to purge; omit them to purge every stored feature", Many: true}}
}
func (p *featurePurgeCommand) Flags(flags *flag.FlagSet) {
flags.Func("except", "Keep this `feature`; pass it once for each", func(name string) error {
p.except = append(p.except, name)
return nil
})
flags.BoolVar(&p.exceptDefined, "except-defined", false, "Keep every feature the application still defines")
}
func (p *featurePurgeCommand) Run(ctx context.Context, c *console.Console) error {
store := p.app.features.Store()
stored, err := store.Names(ctx)
if err != nil {
return err
}
slices.Sort(stored)
stored = slices.Compact(stored)
names := stored
if len(c.Args) > 0 {
names = nil
for _, name := range slices.Compact(slices.Sorted(slices.Values(c.Args))) {
if _, defined := feature.Find(ctx, name); defined || slices.Contains(stored, name) {
names = append(names, name)
continue
}
if similar := similarFeature(ctx, name); similar != "" {
c.Warnf("Feature `%s` isn't defined or stored. Did you mean `%s`?", name, similar)
} else {
c.Warnf("Feature `%s` isn't defined or stored.", name)
}
}
}
names = slices.DeleteFunc(names, func(name string) bool {
_, defined := feature.Find(ctx, name)
return slices.Contains(p.except, name) || p.exceptDefined && defined
})
if len(names) == 0 {
c.Info("No features to purge.")
return nil
}
if err := store.Purge(ctx, names...); err != nil {
return err
}
if _, ok := store.(*feature.MemoryStore); ok {
c.Warnf("Feature store `%s` only reaches this process, so other processes keep their values.", cmp.Or(p.app.Config().Features.Store, "memory"))
}
if len(names) == 1 {
c.Successf("Feature `%s` purged.", names[0])
} else {
c.Successf("%d features purged.", len(names))
}
return nil
}
// similarFeature returns the defined feature whose name differs from name
// only in its hyphens and underscores, such as new-api for new_api.
func similarFeature(ctx context.Context, name string) string {
unify := func(name string) string { return strings.ReplaceAll(name, "-", "_") }
for _, flag := range feature.Defined(ctx) {
if flag.Name() != name && unify(flag.Name()) == unify(name) {
return flag.Name()
}
}
return ""
}