/
hclutils.go
56 lines (48 loc) · 1.3 KB
/
hclutils.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
package hclutils
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/hcl"
hclParser "github.com/hashicorp/hcl/hcl/parser"
hclprinter "github.com/hashicorp/hcl/hcl/printer"
jsonParser "github.com/hashicorp/hcl/json/parser"
"io"
)
// ToJson byte array convert **json** out to io.Writer
func ToJson(input []byte, out io.Writer) error {
v := new(interface{})
astFile, err := hclParser.Parse(input)
if err != nil {
return fmt.Errorf("unable to parse HCL: %s", err)
}
err = hcl.DecodeObject(v, astFile)
if err != nil {
return fmt.Errorf("unable to decodeobject ast.node: %s", err)
}
data, err := json.MarshalIndent(*v, "", " ")
if err != nil {
return fmt.Errorf("unable to marshal json: %s", err)
}
_, err = io.Copy(out, bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("unable to write fiel to %s: %s", out, err)
}
return nil
}
// ToHcl byte array convert **hcl** out to io.Writer
func ToHcl(input []byte, out io.Writer) error {
ast, err := jsonParser.Parse(input)
if err != nil {
return fmt.Errorf("unable to parse JSON: %s", err)
}
var buf bytes.Buffer
if err := hclprinter.Fprint(&buf, ast); err != nil {
return fmt.Errorf("unable to print HCL: %s", err)
}
_, err = io.Copy(out, &buf)
if err != nil {
return fmt.Errorf("unable to print HCL: %s", err)
}
return nil
}