Hclwrite - pkr.hcl editing

Hi,

I’m completely new to GOlang and the mentioned hclwrite package.

I’m trying to manipulate the following pkr.hcl file using the hclwrite GO package:

I parse the config with the ParseConfig() function like so:
hclwrite.ParseConfig(tfSourceFile, "", hcl.Pos{Line: 1, Column: 1}).

I need to be able to:

  • Remove specific variables, which I’ve been able to achieve with the RemoveBlock() function for example like so:
    rootBody.RemoveBlock(rootBody.FirstMatchingBlock("variable", []string{"allowed_inbound_ip_addresses"}))

  • Add variables, in the “top section” with the other variables as to say, so the AppendNewBlock() or AppendBlock() wouldn’t work for me? Since they state that they append the block “to the end of the receiving body”. What would be the way to approach this?

  • Remove/add/manipulate stuff in the source "azure-arm" "build_vhd"{} block. E.g. remove the temp_resource_group_name = "${var.temp_resource_group_name}" completely, change the value of the capture_container_name = "images" and add test = "testValue" etc.

  • Add provisioners, in the build{} block. And I also need to be able to insert those provisioners at specified locations, like before or after specific already present provisioner blocks.

  • Add a post-processor to the end of the build{} block. I’ve already managed to use the AppendNewBlock() function to somewhat achieve this like so:
    postProcessor := rootBody.AppendNewBlock("post-processor", []string{"manifest"}) postProcessorBody := postProcessor.Body() postProcessorBody.SetAttributeValue("testName", cty.StringVal("testValue"))
    But the problem is that it appends to the bottom of the root body, I want to append to the bottom of the build{} block.

So I guess that (at least some of) my problems are:

  • I’m not sure how to work with/divide the different sections/blocks (sorry for my lack of terminology here), e.g. the top section with the variables, the source "azure-arm" "build_vhd"{} block and the build{} block. To be able to manipulate the contents accordingly.

  • Being able to position my added blocks at specific positions.

Input is of course much appreciated!