forked from hashicorp/nomad
/
parse.go
161 lines (128 loc) · 3.15 KB
/
parse.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
package jobspec2
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
hcljson "github.com/hashicorp/hcl/v2/json"
"github.com/hashicorp/nomad/api"
)
func Parse(path string, r io.Reader) (*api.Job, error) {
if path == "" {
if f, ok := r.(*os.File); ok {
path = f.Name()
}
}
var buf bytes.Buffer
_, err := io.Copy(&buf, r)
if err != nil {
return nil, err
}
return ParseWithConfig(&ParseConfig{
Path: path,
Body: buf.Bytes(),
AllowFS: false,
Strict: true,
})
}
func ParseWithConfig(args *ParseConfig) (*api.Job, error) {
args.normalize()
c := newJobConfig(args)
err := decode(c)
if err != nil {
return nil, err
}
normalizeJob(c)
return c.Job, nil
}
type ParseConfig struct {
Path string
BaseDir string
// Body is the HCL body
Body []byte
// AllowFS enables HCL functions that require file system accecss
AllowFS bool
// ArgVars is the CLI -var arguments
ArgVars []string
// VarFiles is the paths of variable data files
VarFiles []string
// Envs represent process environment variable
Envs []string
Strict bool
// parsedVarFiles represent parsed HCL AST of the passed EnvVars
parsedVarFiles []*hcl.File
}
func (c *ParseConfig) normalize() {
if c.BaseDir == "" {
c.BaseDir = filepath.Dir(c.Path)
}
}
func decode(c *jobConfig) error {
config := c.ParseConfig
file, diags := parseHCLOrJSON(config.Body, config.Path)
for _, varFile := range config.VarFiles {
parsedVarFile, ds := parseFile(varFile)
if parsedVarFile == nil || ds.HasErrors() {
return fmt.Errorf("unable to parse var file: %v", ds.Error())
}
config.parsedVarFiles = append(config.parsedVarFiles, parsedVarFile)
diags = append(diags, ds...)
}
// Return early if the input job or variable files are not valid.
// Decoding and evaluating invalid files may result in unexpected results.
if diags.HasErrors() {
return diags
}
diags = append(diags, c.decodeBody(file.Body)...)
if diags.HasErrors() {
var str strings.Builder
for i, diag := range diags {
if i != 0 {
str.WriteByte('\n')
}
str.WriteString(diag.Error())
}
return errors.New(str.String())
}
diags = append(diags, decodeMapInterfaceType(&c.Job, c.EvalContext())...)
diags = append(diags, decodeMapInterfaceType(&c.Tasks, c.EvalContext())...)
diags = append(diags, decodeMapInterfaceType(&c.Vault, c.EvalContext())...)
if diags.HasErrors() {
return diags
}
return nil
}
func parseFile(path string) (*hcl.File, hcl.Diagnostics) {
body, err := ioutil.ReadFile(path)
if err != nil {
return nil, hcl.Diagnostics{
&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Failed to read file",
Detail: fmt.Sprintf("failed to read %q: %v", path, err),
},
}
}
return parseHCLOrJSON(body, path)
}
func parseHCLOrJSON(src []byte, filename string) (*hcl.File, hcl.Diagnostics) {
if isJSON(src) {
return hcljson.Parse(src, filename)
}
return hclsyntax.ParseConfig(src, filename, hcl.Pos{Line: 1, Column: 1})
}
func isJSON(src []byte) bool {
for _, c := range src {
if c == ' ' {
continue
}
return c == '{'
}
return false
}