forked from stackrox/kube-linter
/
registry.go
39 lines (33 loc) · 874 Bytes
/
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
package templates
import (
"fmt"
"sort"
"github.com/slysunkin/kube-linter/lib/check"
)
var (
allTemplates = make(map[string]check.Template)
)
// Register registers a template with the given name.
// Intended to be called at program init time.
func Register(t check.Template) {
if _, ok := allTemplates[t.Key]; ok {
panic(fmt.Sprintf("duplicate template: %v", t.Key))
}
allTemplates[t.Key] = t
}
// Get gets a template by name, returning a boolean indicating whether it was found.
func Get(name string) (check.Template, bool) {
t, ok := allTemplates[name]
return t, ok
}
// List returns all known templates, sorted by name.
func List() []check.Template {
out := make([]check.Template, 0, len(allTemplates))
for _, t := range allTemplates {
out = append(out, t)
}
sort.Slice(out, func(i, j int) bool {
return out[i].Key < out[j].Key
})
return out
}