-
Notifications
You must be signed in to change notification settings - Fork 16
/
read.go
170 lines (153 loc) · 4.53 KB
/
read.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package dynaml
import (
"path"
"regexp"
"strings"
"github.com/mandelsoft/spiff/debug"
"github.com/mandelsoft/spiff/yaml"
)
var templ_pattern = regexp.MustCompile(".*\\s+&template(\\(?|\\s+).*")
func func_read(cached bool, arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if len(arguments) > 2 {
return info.Error("read takes a maximum of two arguments")
}
if !binding.GetState().FileAccessAllowed() {
return info.DenyOSOperation("read")
}
file, ok := arguments[0].(string)
if !ok {
return info.Error("string value required for file path")
}
t := "text"
if strings.HasSuffix(file, ".yml") || strings.HasSuffix(file, ".yaml") || strings.HasSuffix(file, ".json") {
t = "yaml"
}
if len(arguments) > 1 {
t, ok = arguments[1].(string)
if !ok {
return info.Error("string value required for type")
}
}
data, err := binding.GetFileContent(file, cached)
if err != nil {
return info.Error("read: %s", err)
}
return ParseData(file, data, t, binding)
}
func ParseData(file string, data []byte, mode string, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
info.Source = file
rerooted := binding
if strings.HasPrefix(mode, ".") {
rerooted = binding.WithNewRoot()
mode = mode[1:]
}
switch mode {
case "template":
n, err := yaml.Parse(file, data)
if err != nil {
return info.Error("error parsing file [%s]: %s", path.Clean(file), err)
}
return asTemplate(n, binding), info, true
case "templates":
nodes, err := yaml.ParseMulti(file, data)
if err != nil {
return info.Error("error parsing file [%s]: %s", path.Clean(file), err)
}
result := []yaml.Node{}
for _, n := range nodes {
result = append(result, NewNode(asTemplate(n, binding), n))
}
return result, info, true
case "yaml":
node, err := yaml.Parse(file, data)
if err != nil {
return info.Error("error parsing file [%s]: %s", path.Clean(file), err)
}
debug.Debug("resolving yaml file\n")
result, state := rerooted.Flow(node, false)
if state != nil {
debug.Debug("resolving yaml file failed: %s", state.Error())
return info.PropagateError(nil, state, "resolution of yaml file '%s' failed", file)
}
debug.Debug("resolving yaml file succeeded")
return result.Value(), info, true
case "multiyaml":
nodes, err := yaml.ParseMulti(file, data)
if err != nil {
return info.Error("error parsing file [%s]: %s", path.Clean(file), err)
}
for len(nodes) > 1 && nodes[len(nodes)-1].Value() == nil {
nodes = nodes[:len(nodes)-1]
}
debug.Debug("resolving yaml list from file\n")
result, state := rerooted.Flow(NewNode(nodes, info), false)
if state != nil {
debug.Debug("resolving yaml file failed: " + state.Error())
return info.PropagateError(nil, state, "resolution of yaml file '%s' failed", file)
}
debug.Debug("resolving yaml file succeeded")
return result.Value(), info, true
case "import":
node, err := yaml.Parse(file, data)
if err != nil {
return info.Error("error parsing file [%s]: %s", path.Clean(file), err)
}
info.Raw = true
debug.Debug("import yaml file succeeded")
return node.Value(), info, true
case "importmulti":
nodes, err := yaml.ParseMulti(file, data)
if err != nil {
return info.Error("error parsing file [%s]: %s", path.Clean(file), err)
}
info.Raw = true
for len(nodes) > 1 && nodes[len(nodes)-1].Value() == nil {
nodes = nodes[:len(nodes)-1]
}
return nodes, info, true
case "text":
return string(data), info, true
case "binary":
return Base64Encode(data, 60), info, true
default:
return info.Error("invalid file type [%s] %s", path.Clean(file), mode)
}
}
func asTemplate(n yaml.Node, binding Binding) TemplateValue {
orig := node_copy(n)
switch v := orig.Value().(type) {
case map[string]yaml.Node:
if _, ok := v[yaml.MERGEKEY]; !ok {
v[yaml.MERGEKEY] = NewNode("(( &template ))", n)
} else {
if _, ok := v["<<"]; !ok {
v["<<"] = NewNode("(( &template ))", n)
}
}
case []yaml.Node:
found := false
for _, e := range v {
if m, ok := e.Value().(map[string]yaml.Node); ok {
e, ok := m[yaml.MERGEKEY]
if !ok {
e, ok = m["<<"]
}
if ok {
s := yaml.EmbeddedDynaml(e, binding.GetState().InterpolationEnabled())
if s != nil && templ_pattern.MatchString(*s) {
found = true
break
}
}
}
}
if !found {
new := []yaml.Node{NewNode(map[string]yaml.Node{yaml.MERGEKEY: NewNode("(( &template ))", n)}, n)}
new = append(new, v...)
orig = NewNode(new, n)
}
}
return NewTemplateValue(binding.Path(), n, orig, binding)
}