-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcwl.go
114 lines (95 loc) · 2.12 KB
/
cwl.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
package cwl
import (
"fmt"
"github.com/commondream/yamlast"
"io/ioutil"
)
func Load(loc string) (Document, error) {
return LoadWithResolver(loc, DefaultResolver{})
}
func LoadWithResolver(loc string, r Resolver) (Document, error) {
if r == nil {
r = NoResolve()
}
var b []byte
var base string
var err error
// If NoResolve() is being used, load the document bytes using
// the default resolver, but then continue with NoResolve().
if _, ok := r.(noResolver); ok {
d := DefaultResolver{}
b, base, err = d.Resolve("", loc)
} else {
b, base, err = r.Resolve("", loc)
}
if err != nil {
return nil, fmt.Errorf("failed to resolve document: %s", err)
}
return LoadDocumentBytes(b, base, r)
}
func LoadDocumentBytes(b []byte, base string, r Resolver) (Document, error) {
if r == nil {
r = NoResolve()
}
l := loader{base, r}
// Parse the YAML into an AST
yamlnode, err := yamlast.Parse(b)
if err != nil {
return nil, fmt.Errorf("parsing yaml: %s", err)
}
if yamlnode == nil {
return nil, fmt.Errorf("empty yaml")
}
if len(yamlnode.Children) > 1 {
return nil, fmt.Errorf("unexpected child count")
}
// Being recursively processing the tree.
var d Document
start := node(yamlnode.Children[0])
start, err = l.preprocess(start)
if err != nil {
return nil, err
}
// Dump the tree for debugging.
//dump(start, "")
err = l.load(start, &d)
if err != nil {
return nil, err
}
if d != nil {
return d, nil
}
return nil, nil
}
func LoadValuesFile(p string) (Values, error) {
b, err := ioutil.ReadFile(p)
if err != nil {
return nil, err
}
return LoadValuesBytes(b)
}
func LoadValuesBytes(b []byte) (Values, error) {
l := loader{}
// Parse the YAML into an AST
yamlnode, err := yamlast.Parse(b)
if err != nil {
return nil, fmt.Errorf("parsing yaml: %s", err)
}
v := Values{}
if yamlnode == nil {
return v, nil
}
if len(yamlnode.Children) > 1 {
return nil, fmt.Errorf("unexpected child count")
}
start := node(yamlnode.Children[0])
start, err = l.preprocess(start)
if err != nil {
return nil, err
}
err = l.load(start, &v)
if err != nil {
return nil, err
}
return v, nil
}