-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwalk.go
87 lines (76 loc) · 1.77 KB
/
walk.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
package kommons
import (
"os"
"path/filepath"
"strings"
"github.com/flanksource/commons/logger"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type Specs []Spec
type Spec struct {
Path string
Items []*unstructured.Unstructured
}
// Walk iterates recursively over each file in path and
// returns all of the objects contained
func Walk(path string) (Specs, error) {
specs := Specs{}
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, "kustomization.yaml") || strings.HasSuffix(path, "kustomization.yml") {
return nil
}
if !strings.HasSuffix(path, ".yml") && !strings.HasSuffix(path, ".yaml") {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
items, err := GetUnstructuredObjects(data)
if err != nil {
logger.Errorf("Error decoding %s: %v", path, err)
return nil
}
items, err = Unwrap(items)
if err != nil {
return nil
}
specs = append(specs, Spec{
Path: path,
Items: items,
})
return nil
})
return specs, err
}
func Unwrap(list []*unstructured.Unstructured) ([]*unstructured.Unstructured, error) {
var items []*unstructured.Unstructured
for _, item := range list {
if item.IsList() {
children, err := item.ToList()
if err != nil {
return nil, err
}
for _, child := range children.Items {
items = append(items, &child)
}
} else {
items = append(items, item)
}
}
return items, nil
}
func (specs Specs) FilterBy(kind string) []*unstructured.Unstructured {
items := []*unstructured.Unstructured{}
for _, spec := range specs {
for _, item := range spec.Items {
if strings.EqualFold(item.GetKind(), kind) {
items = append(items, item)
}
}
}
return items
}