-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
183 lines (163 loc) · 4.45 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
const (
jsonFormat = "json"
hclFormat = "hcl"
)
func main() {
// Command-line flags
inputFile := flag.String("i", "", "Input file path")
outputFile := flag.String("o", "", "Output file path")
reverse := flag.Bool("r", false, "Reverse input/output format")
version := flag.Bool("v", false, "Show version")
help := flag.Bool("h", false, "Show help")
flag.Parse()
if *version {
fmt.Println("json2hcl2 version 1.0")
return
}
if *help {
flag.Usage()
return
}
inputData, err := readInputData(*inputFile)
if err != nil {
log.Fatalf("Failed to read input: %s", err)
}
outputData, err := convertData(inputData, *reverse)
if err != nil {
log.Fatalf("Failed to convert data: %s", err)
}
if *outputFile == "" {
writeOutputData(os.Stdout, outputData)
} else {
err = writeOutputFile(*outputFile, outputData)
if err != nil {
log.Fatalf("Failed to write output file: %s", err)
}
}
}
func readInputData(inputFile string) ([]byte, error) {
if inputFile == "" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(inputFile)
}
func convertData(inputData []byte, reverse bool) ([]byte, error) {
if reverse {
return convertToJSON(inputData)
}
return convertToHCL(inputData)
}
func parse(reverse bool, inputData []byte, filename string) (*hcl.File, error) {
parser := hclparse.NewParser()
if reverse {
parsedFile, diag := parser.ParseJSON(inputData, filename)
if diag != nil {
return nil, fmt.Errorf("failed to parse JSON: %s", diag.Error())
}
return parsedFile, diag
} else {
parsedFile, diag := parser.ParseHCL(inputData, filename)
if diag != nil {
return nil, fmt.Errorf("failed to parse HCL: %s", diag.Error())
}
return parsedFile, diag
}
}
func convertToJSON(inputData []byte) ([]byte, error) {
hclFile, _ := parse(false, inputData, hclFormat)
if _, diag := hclFile.Body.JustAttributes(); !diag.HasErrors() {
file := hclwrite.NewEmptyFile()
return hclToJSON(file)
} else {
return nil, fmt.Errorf("failed to parse JSON: %s", diag.Error())
}
}
func convertToHCL(inputData []byte) ([]byte, error) {
var obj map[string]interface{}
hclFile, _ := parse(true, inputData, jsonFormat)
if _, diag := hclFile.Body.JustAttributes(); !diag.HasErrors() {
err := json.Unmarshal(inputData, &obj)
if err != nil {
return nil, fmt.Errorf("failed to Unmarshal JSON: %s", err)
}
file := hclwrite.NewEmptyFile()
jsonToHCL(file.Body(), obj)
return file.Bytes(), nil
} else {
return nil, fmt.Errorf("failed to parse JSON: %s", diag.Error())
}
}
func writeOutputData(outputStream *os.File, outputData []byte) {
outputStream.Write(outputData)
}
func writeOutputFile(outputFile string, outputData []byte) error {
return ioutil.WriteFile(outputFile, outputData, 0644)
}
func hclToJSON(file *hclwrite.File) ([]byte, error) {
return json.MarshalIndent(file.Body().BuildTokens(nil), "", " ")
}
func jsonToHCL(body *hclwrite.Body, obj map[string]interface{}) {
for key, value := range obj {
switch value.(type) {
case string, bool, float64, int64:
body.SetAttributeValue(key, ctyFromValue(value))
case []string, []float64, []int64:
body.SetAttributeValue(key, ctyFromValue(value))
case []interface{}:
body.SetAttributeValue(key, ctyFromValue(value))
case map[string]interface{}:
body.SetAttributeValue(key, ctyFromValue(value))
}
}
}
func ctyFromJson(value interface{}) cty.Value {
jsonVal, err := json.Marshal(value)
if err != nil {
log.Fatalf("failed to marshal json value: %s", err.Error())
return cty.NilVal
}
ctyType, err := ctyjson.ImpliedType(jsonVal)
if err != nil {
log.Fatalf("failed to get ctyType: %s", err.Error())
return cty.NilVal
}
ctyVal, err := ctyjson.Unmarshal(jsonVal, ctyType)
if err != nil {
log.Fatalf("failed to get ctyVal: %s", err.Error())
return cty.NilVal
}
return ctyVal
}
func ctyFromValue(value interface{}) cty.Value {
switch v := value.(type) {
case string:
return cty.StringVal(v)
case bool:
return cty.BoolVal(v)
case float64:
return cty.NumberFloatVal(v)
case int64:
return cty.NumberIntVal(v)
case []interface{}:
return ctyFromJson(value)
case []string, []float64, []int64:
return ctyFromJson(value)
case map[string]interface{}:
return ctyFromJson(value)
}
return cty.NilVal
}