Closed x509cert closed 3 years ago
I have a POC of how to use HCL here. I am still learning to use it myself so I'll be hoping for more advanced uses cases from someone else: https://github.com/DavidGamba/go-wardley/blob/master/hcl/hcl.go
Basically:
parser := hclparse.NewParser()
hclFIle, diags := parser.ParseHCLFile(filename)
diags := gohcl.DecodeBody(hclFile.Body, ctx, &targetStruct)
To Parse HCL file you need a target struct and the hcl file path
config := TargetStruct{}
err := hclsimple.DecodeFile(filepath, nil, &config)
I recently ran into a wall here myself. It turns out that terraform is not "simple" hcl, terraform code is doing custom work in back. I posted this question in Stack Overflow and got fantastic help. It may pull inspiration from there.
Working code for at least part of a variable block. https://stackoverflow.com/questions/66620096/unable-to-read-terraform-variables-tf-files-into-may-go-program
So I have encountered the same issue, after upgrading my TF from version 0.11 to 0.13 where HCL2 comes in place. I just don't know how to parse the .tf file any more as previously we were just using Decode or Unmarshal method of hcl package. I have given the detailed description of my issue over here. https://github.com/hashicorp/hcl/issues/468
Hi all,
Unfortunately there can't really be a simple example for parsing the Terraform language because the Terraform language isn't simple: it's likely the most complicated application of HCL, making use of various advanced features that are originally motivated by meeting Terraform use-cases.
The Terraform team maintains terraform-config-inspect as an external library for extracting top-level objects from a Terraform module. If you can get the information you need using that library then I would recommend to use it and keep it upgraded as new versions are tagged to ensure that you'll always be able to parse the latest version of the Terraform language, along with backward compatibility with earlier versions of the language.
If you need more than terraform-config-inspect provides then you could perhaps use that library as an example to get started, along with referring to Terraform's own configuration loader, but note then that you may need to do ongoing new development in your loader to support Terraform language features added later.
Since this repository is about HCL itself rather than about Terraform, and since it's not really clear how to meet this request anyway given what I've said above, I'm going to close this. If you want to discuss more some techniques for parsing Terraform configurations in external tools then I'd suggest starting a topic in Terraform's community forum, which is a good place for open-ended general discussion about Terraform, as opposed to bugs and feature requests about Terraform or the libraries it depends on. Thanks!
I am new to parsing HCL, so I figured I might have a somewhat naive view of the topic. It's hard for someone like me to understand how to parse a TF file (for example) and walk the tree looking for something of interest.. so if I start with a file and then get to this:
ast, err := hcl.Parse(string(dat))
what's next? how do I walk the AST?
If people can point me in the right direction, I will happily write the code and supporting text.
Thanks