Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hclwrite: SetAttributeRaw() after Clear() is not present in hcl.Bytes() output #623

Open
ElvenSpellmaker opened this issue Aug 18, 2023 · 0 comments

Comments

@ElvenSpellmaker
Copy link

I'm writing some code to take in an HCL file (Packer in this case) and perform some transformations on it (it's a shame the JSON is deprecated as this was an easier format to parse, but that's another thing).

I'm not sure if it's something I'm not doing right or if this is perhaps a bug?

I get the attributes attrs := build.Body().Attributes() (build is a Block within the HCL file) and then clear the build (build.Body().Clear()) element for injecting the extra blocks I need.

Obviously to get it back to the same state I need to now re-set the Attributes and munged Blocks onto the build Block.

I do this with the following (for Attributes):

for attrName, attrValue := range attrs {
		build.Body().SetAttributeRaw(attrName, attrValue.Expr().BuildTokens(hclwrite.Tokens{}))
		build.Body().AppendNewline()
}

I can then get the new Attributes print the result of the Attributes method and see the correct output:

attrs = build.Body().Attributes()
for attrName, attrValue := range attrs {
	fmt.Printf("%s = %s", attrName, attrValue.Expr().BuildTokens(hclwrite.Tokens{}).Bytes())
}

Output: sources = ["source.azure-arm.build_vhd"]

This successfully prints Attributes, however when I go to then write the file back out, the Attributes are missing:

os.WriteFile("output.hcl", hcl.Bytes(), 0644)

Expecting:
image

Received:
image

It seems somehow by setting Attributes there on the Body element but don't get considered when written back out to a file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant