forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
85 lines (78 loc) · 2.72 KB
/
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
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
// Copyright (C) 2021-2023 Chronicle Labs, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package hcl
import (
"fmt"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
// ParseFiles parses the HCL configuration files at the given paths. It returns
// a merged hcl.Body. The subject argument is optional. It is used to provide a
// range for the returned diagnostics.
func ParseFiles(paths []string, subject *hcl.Range) (hcl.Body, hcl.Diagnostics) {
bodies := make([]hcl.Body, len(paths))
for n, path := range paths {
body, diags := ParseFile(path, subject)
if diags.HasErrors() {
return nil, diags
}
bodies[n] = body
}
return hcl.MergeBodies(bodies), nil
}
// ParseFile parses the given path into a hcl.File. The subject argument is
// optional. It is used to provide a range for the returned diagnostics.
func ParseFile(path string, subject *hcl.Range) (hcl.Body, hcl.Diagnostics) {
src, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Failed to read configuration",
Detail: fmt.Sprintf("Cannot read file %s: file does not exist.", path),
Subject: subject,
}}
}
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Failed to read configuration",
Detail: fmt.Sprintf("Cannot read file %s: %s.", path, err),
Subject: subject,
}}
}
return ParseSource(path, src)
}
// ParseSources parses the HCL configuration sources. It returns a merged
// hcl.Body.
func ParseSources(srcs [][]byte) (hcl.Body, hcl.Diagnostics) {
bodies := make([]hcl.Body, len(srcs))
for n := 0; n < len(srcs); n++ {
body, diags := ParseSource(fmt.Sprintf("embeded #%d", n), srcs[n])
if diags.HasErrors() {
return nil, diags
}
bodies[n] = body
}
return hcl.MergeBodies(bodies), nil
}
// ParseSource parses the given source into a hcl.File.
func ParseSource(name string, src []byte) (hcl.Body, hcl.Diagnostics) {
file, diags := hclsyntax.ParseConfig(src, name, hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
return nil, diags
}
return file.Body, nil
}