-
Notifications
You must be signed in to change notification settings - Fork 0
/
loaders.go
120 lines (111 loc) · 2.79 KB
/
loaders.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package yamltools
import (
"gopkg.in/yaml.v3"
"io/fs"
"os"
"path/filepath"
)
type TagProcessor = func(n *yaml.Node) error
// HandleCustomTag is used to define a custom YAML tags, it will recursively search YAML nodes
// for the tag and call the tag processor function.
func HandleCustomTag(n *yaml.Node, tag string, fn TagProcessor) error {
if n.Tag == tag {
err := fn(n)
if err != nil {
return err
}
} else {
if n.Kind == yaml.SequenceNode {
for _, n := range n.Content {
err := HandleCustomTag(n, tag, fn)
if err != nil {
return err
}
}
} else if n.Kind == yaml.MappingNode {
// only need to check every second node (the values)
for i := 1; i < len(n.Content); i += 2 {
err := HandleCustomTag(n.Content[i], tag, fn)
if err != nil {
return err
}
}
}
}
return nil
}
// Fragment is used to parse YAML into a node instead of an interface.
type Fragment struct {
Content *yaml.Node
}
func (f *Fragment) UnmarshalYAML(n *yaml.Node) error {
f.Content = n
return nil
}
// LoadFileFragment reads in and parses a given file returning a YAML node.
func LoadFileFragment(path string) (*yaml.Node, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var f Fragment
err = yaml.Unmarshal(data, &f)
if err != nil {
return nil, err
}
return f.Content, nil
}
// LoadIncludeTag recursively searches for the !include tag from the given node
// and will replace the tag node with content of the included file.
func LoadIncludeTag(n *yaml.Node) error {
return HandleCustomTag(n, "!include", func(n *yaml.Node) error {
fragment, err := LoadFileFragment(n.Value)
if err != nil {
return err
}
*n = *fragment
return nil
})
}
// LoadIncludeDirNamedTag recursively searches for the !include_dir_named tag from the given node
// and will replace the tag node with map of filename to content for each file in the directory.
func LoadIncludeDirNamedTag(n *yaml.Node) error {
return HandleCustomTag(n, "!include_dir_named", func(n *yaml.Node) error {
content := make([]*yaml.Node, 0, 10)
err := filepath.WalkDir(n.Value, func(path string, entry fs.DirEntry, err error) error {
if path == n.Value {
return nil
}
if entry.IsDir() {
return nil
}
fragment, err := LoadFileFragment(path)
if err != nil {
return err
}
content = append(content, &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!str",
Value: fileNameWithoutExt(filepath.Base(path)),
}, fragment)
return nil
})
if err != nil {
return err
}
*n = *&yaml.Node{
Kind: yaml.MappingNode,
Tag: "!!map",
Content: content,
}
return nil
})
}
func fileNameWithoutExt(path string) string {
for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
if path[i] == '.' {
return path[:i]
}
}
return ""
}