-
Notifications
You must be signed in to change notification settings - Fork 0
/
values_file.go
55 lines (48 loc) · 1.29 KB
/
values_file.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
package config
import (
"fmt"
"io/ioutil"
"os"
"github.com/hashicorp/hcl2/hcl"
)
func (p *Parser) ParseValuesFiles(filenames ...string) (hcl.Attributes, hcl.Diagnostics) {
attrs := make(hcl.Attributes)
var diags hcl.Diagnostics
for _, filename := range filenames {
src, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Failed to read values from file",
Detail: fmt.Sprintf("The requested file %s does not exist.", filename),
},
}
} else {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Failed to read values from file",
Detail: fmt.Sprintf("There was an error reading %s: %s", filename, err),
},
}
}
}
thisAttrs, thisDiags := p.ParseValuesSource(src, filename)
diags = append(diags, thisDiags...)
for k, v := range thisAttrs {
attrs[k] = v
}
}
return attrs, diags
}
func (p *Parser) ParseValuesSource(src []byte, filename string) (hcl.Attributes, hcl.Diagnostics) {
astFile, diags := p.HCLParser.ParseHCL(src, filename)
if astFile == nil {
return make(hcl.Attributes), diags
}
attrs, decDiags := astFile.Body.JustAttributes()
diags = append(diags, decDiags...)
return attrs, diags
}