I am attempting to write a go program that reads in a terraform variables.tf
and populates a struct for later manipulation. However, I am getting errors when attempting to "parse" the file. I Am hoping someone can tell me what I am doing wrong:
Code:
package main import ( "fmt" "io/ioutil" "log" "os" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/hclsyntax" ) type Config struct { Upstreams []*TfVariable `hcl:"variable,block"` } type TfVariable struct { Name string `hcl:",label"` // Default string `hcl:"default,optional"` Type string `hcl:"type"` Description string `hcl:"description,attr"` // validation block Sensitive bool `hcl:"sensitive,optional"` } func main() { readHCLFile("examples/string.tf") } // Exits program by sending error message to standard error and specified error code. func abort(errorMessage string, exitcode int) { fmt.Fprintln(os.Stderr, errorMessage) os.Exit(exitcode) } func readHCLFile(filePath string) { content, err := ioutil.ReadFile(filePath) if err != nil { log.Fatal(err) } fmt.Printf("File contents: %s", content) // TODO: Remove me file, diags := hclsyntax.ParseConfig(content, filePath, hcl.Pos{Line: 1, Column: 1}) if diags.HasErrors() { log.Fatal(fmt.Errorf("ParseConfig: %w", diags)) } c := &Config{} diags = gohcl.DecodeBody(file.Body, nil, c) if diags.HasErrors() { log.Fatal(fmt.Errorf("DecodeBody: %w", diags)) } fmt.Println(c) // TODO: Remove me }
ERROR
File contents: variable "image_id" { type = string description = "The id of the machine image (AMI) to use for the server." sensitive = false } variable "other_id" { type = string description = "The id of the machine image (AMI) to use for the server." sensitive = true } 2021/03/13 19:55:49 DecodeBody: examples/string.tf:2,17-23: Variables not allowed; Variables may not be used here., and 3 other diagnostic(s) exit status 1
Stack driver question is sadly for hcl1
Blog post I am referencing.
https://stackoverflow.com/questions/66620096/unable-to-read-terraform-variables-tf-files-into-may-go-program March 14, 2021 at 09:02AM
没有评论:
发表评论