-
Notifications
You must be signed in to change notification settings - Fork 31
/
io.go
66 lines (60 loc) · 1.47 KB
/
io.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
package util
import (
"io/fs"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)
func ReadFile(filename string) (*unstructured.UnstructuredList, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return SplitYAML(data)
}
func SplitYAML(v interface{}) (*unstructured.UnstructuredList, error) {
switch data := v.(type) {
case []byte:
list := &unstructured.UnstructuredList{}
for _, text := range strings.Split(string(data), "---") {
item := unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(text), &item); err != nil {
return nil, err
}
list.Items = append(list.Items, item)
}
return list, nil
case string:
return SplitYAML([]byte(data))
default:
panic("unknown type")
}
}
type FileInfoUnstructuredList struct {
fs.FileInfo
unstructured.UnstructuredList
}
func ReadDir(dirname string) ([]FileInfoUnstructuredList, error) {
infos, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, err
}
sort.Slice(infos, func(i, j int) bool {
return strings.Compare(infos[i].Name(), infos[j].Name()) < 0
})
var x []FileInfoUnstructuredList
for _, info := range infos {
if !strings.HasSuffix(info.Name(), ".yaml") {
continue
}
list, err := ReadFile(filepath.Join(dirname, info.Name()))
if err != nil {
return nil, err
}
x = append(x, FileInfoUnstructuredList{FileInfo: info, UnstructuredList: *list})
}
return x, nil
}