-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (51 loc) · 1.09 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/hashicorp/hcl2/hclwrite"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
var (
source = flag.String("source", "", "--source=./path/to/source.json")
)
func inputFromReader(r io.Reader) (map[string]interface{}, error) {
var out map[string]interface{}
dec := json.NewDecoder(r)
for {
err := dec.Decode(&out)
if err == io.EOF {
break
}
return out, err
}
return out, nil
}
func main() {
flag.Parse()
// default to read the data from stdin
inputSource := os.Stdin
if *source != "" {
var err error
inputSource, err = os.Open(*source)
if err != nil {
log.Fatal("Unable to open source file", err)
}
}
input, err := inputFromReader(inputSource)
if err != nil {
log.Fatal("Unable to load source", err)
}
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
for k, v := range input {
raw, _ := json.Marshal(v)
simple := &ctyjson.SimpleJSONValue{}
json.Unmarshal(raw, simple)
rootBody.SetAttributeValue(k, simple.Value)
}
fmt.Fprintf(os.Stdout, "%s", hclwrite.Format(f.Bytes()))
}