hashicorp / hashicorp/hcl

Updating Attribute in place results in duplicate attribute being appended to end of file

Open
#484 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

i'm fairly new to golang so please forgive my ignorance if the below code is incorrect. I'm trying to update an attribute in place within an tfvars file. However, whenever I set the attribute value, build the tokens and write to the file a duplicate attribute with updated values is appended to the bottom of the file. Below is some sample code:

newSlice := []string{"a", "b", "c", "d"}
file, err := os.OpenFile(`file.tfvars`, os.O_RDWR, 0755)
defer file.Close()
CheckError(err)
src, err := ioutil.ReadAll(file)
CheckError(err)
conf, diags := hclwrite.ParseConfig(src, `file.tfvars`, hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
log.Println(diags)
}
rootBody := conf.Body()
ctSlice, err := gocty.ToCtyValue(newSlice, cty.List(cty.String))
CheckError(err)
rootBody.SetAttributeValue(`attribute_to_update`, cty.ListVal(ctSlice.AsValueSlice())).BuildTokens(nil).WriteTo(file)

any help would be greatly appreciated, Thank you

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.