-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.go
68 lines (56 loc) · 1.06 KB
/
yaml.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
package conflint
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
yaml "gopkg.in/yaml.v3"
)
func ReadYAMLFiles(f string) (map[string][]yaml.Node, error) {
var files []string
stat, err := os.Stat(f)
if err != nil {
return nil, err
}
if stat.IsDir() {
matches, err := filepath.Glob(filepath.Join(f, "*"))
if err != nil {
return nil, err
}
files = append(files, matches...)
} else {
files = append(files, f)
}
res := map[string][]yaml.Node{}
for _, f := range files {
nodes := []yaml.Node{}
var reader io.Reader
if f == "-" {
reader = os.Stdin
} else if f != "" {
fp, err := os.Open(f)
if err != nil {
return nil, err
}
reader = fp
defer fp.Close()
} else {
return nil, fmt.Errorf("Nothing to eval: No file specified")
}
buf := bufio.NewReader(reader)
decoder := yaml.NewDecoder(buf)
for {
node := yaml.Node{}
if err := decoder.Decode(&node); err != nil {
if err != io.EOF {
return nil, err
}
break
}
nodes = append(nodes, node)
}
res[f] = nodes
}
return res, nil
}