forked from argoproj-labs/argocd-vault-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
67 lines (56 loc) · 1.5 KB
/
util.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
package cmd
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8yaml "k8s.io/apimachinery/pkg/util/yaml"
)
func listFiles(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".yaml" || filepath.Ext(path) == ".yml" || filepath.Ext(path) == ".json" {
files = append(files, path)
}
return nil
})
if err != nil {
return files, err
}
return files, nil
}
func readFilesAsManifests(paths []string) (result []unstructured.Unstructured, errs []error) {
for _, path := range paths {
rawdata, err := os.ReadFile(path)
if err != nil {
errs = append(errs, fmt.Errorf("could not read file: %s from disk: %s", path, err))
}
manifest, err := readManifestData(bytes.NewReader(rawdata))
if err != nil {
errs = append(errs, fmt.Errorf("could not read file: %s from disk: %s", path, err))
}
result = append(result, manifest...)
}
return result, errs
}
func readManifestData(yamlData io.Reader) ([]unstructured.Unstructured, error) {
decoder := k8yaml.NewYAMLOrJSONDecoder(yamlData, 1)
var manifests []unstructured.Unstructured
for {
nxtManifest := unstructured.Unstructured{}
err := decoder.Decode(&nxtManifest)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
// Skip empty manifests
if len(nxtManifest.Object) > 0 {
manifests = append(manifests, nxtManifest)
}
}
return manifests, nil
}