I want to create a .tf
file by executing a Golang script using hashicorp/hcl2
.
var "level_1_apis" {
default = [
{
api_name = "service-1-strict-hello",
endpoint = "/service-1/strict/hello"
}
]
}
I am parsing by this Golang struct.
type Root struct {
Resources []Resource `hcl:"resource,block"`
Variables []Variable `hcl:"var,block"`
}
// Resource ...
type Resource struct {
Type string `hcl:"type,label"`
Name string `hcl:"name,label"`
}
// Variable ...
type Variable struct {
Type string `hcl:"type,label"`
Default hcl.Expression `hcl:"default"`
}
I am decoding this by hclparser.ParseHCLFile
. I can access to my variables.
How can I do the opposite?
I ran the code below but the output has no default field.
gohcl.EncodeIntoBody(root, rootBody)
if err := ioutil.WriteFile("./hoge.tf", f.Bytes(), 0644); err != nil {
return err
}
var "service-1-3" {
}