-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathregistry.go
81 lines (67 loc) · 1.63 KB
/
registry.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
package launcher
import (
"sort"
"strings"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var AppRegistry = map[string]*AppDef{}
func RegisterApp(logger *zap.Logger, appDef *AppDef) {
logger.Debug("registering app", zap.Stringer("app", appDef))
AppRegistry[appDef.ID] = appDef
}
var RegisterCommonFlags func(logger *zap.Logger, cmd *cobra.Command) error
func RegisterFlags(logger *zap.Logger, cmd *cobra.Command) error {
for _, appDef := range AppRegistry {
logger.Debug("trying to register flags", zap.String("app_id", appDef.ID))
if appDef.RegisterFlags != nil {
logger.Debug("found non nil flags, registering", zap.String("app_id", appDef.ID))
err := appDef.RegisterFlags(cmd)
if err != nil {
return err
}
}
}
if RegisterCommonFlags != nil {
if err := RegisterCommonFlags(logger, cmd); err != nil {
return err
}
}
return nil
}
func ParseAppsFromArgs(args []string, runByDefault func(string) bool) (apps []string) {
if len(args) == 0 {
return ParseAppsFromArgs([]string{"all"}, runByDefault)
}
for _, arg := range args {
chunks := strings.Split(arg, ",")
for _, app := range chunks {
app = strings.TrimSpace(app)
if app == "all" {
for app := range AppRegistry {
if !runByDefault(app) {
continue
}
apps = append(apps, app)
}
} else {
if strings.HasPrefix(app, "-") {
removeApp := app[1:]
apps = removeElement(apps, removeApp)
} else {
apps = append(apps, app)
}
}
}
}
sort.Strings(apps)
return
}
func removeElement(lst []string, el string) (out []string) {
for _, l := range lst {
if l != el {
out = append(out, l)
}
}
return
}