-
Notifications
You must be signed in to change notification settings - Fork 10
/
hcl.go
111 lines (90 loc) · 2.2 KB
/
hcl.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
package configure
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"github.com/hashicorp/hcl"
)
// NewHCL returns an instance of the HCL checker. It takes a function
// which returns an io.Reader which will be called when the first value
// is recalled. The contents of the io.Reader MUST be decodable into HCL or JSON.
func NewHCL(gen func() (io.Reader, error)) *HCL {
return &HCL{
gen: gen,
}
}
// NewHCLFromFile returns an instance of the HCL checker. It reads its
// data from a file (file content can be HCL or JSON) which its location has been specified through the path
// parameter
func NewHCLFromFile(path string) *HCL {
return NewHCL(func() (io.Reader, error) {
return os.Open(path)
})
}
// HCL represents the HCL Checker. It reads an io.Reader and then pulls a value out of a map[string]interface{}.
type HCL struct {
values map[string]interface{}
gen func() (io.Reader, error)
}
// Setup initializes the HCL Checker
func (h *HCL) Setup() error {
r, err := h.gen()
if err != nil {
return err
}
buf := new(bytes.Buffer)
buf.ReadFrom(r)
s := buf.String()
//first parse the hcl file
obj, err := hcl.Parse(s)
if err != nil {
return err
}
h.values = make(map[string]interface{})
// then decode the object
if err = hcl.DecodeObject(&h.values, obj); err != nil {
return err
}
return nil
}
func (h *HCL) value(name string) (interface{}, error) {
val, ok := h.values[name]
if !ok {
return nil, errors.New("variable does not exist")
}
return val, nil
}
// Int returns an int if it exists within the HCL io.Reader
func (h *HCL) Int(name string) (int, error) {
v, err := h.value(name)
if err != nil {
return 0, err
}
f, ok := v.(float64)
if !ok {
i, ok := v.(int)
if !ok {
return v.(int), errors.New(fmt.Sprintf("%T unable", v))
}
return i, nil
}
return int(f), nil
}
// Bool returns a bool if it exists within the HCL io.Reader.
func (h *HCL) Bool(name string) (bool, error) {
v, err := h.value(name)
if err != nil {
return false, err
}
return v.(bool), nil
}
// String returns a string if it exists within the HCL io.Reader.
func (h *HCL) String(name string) (string, error) {
v, err := h.value(name)
if err != nil {
return "", err
}
return v.(string), nil
}