hclwrite: Consider Attribute Rename Support
- Dominant language
- Go
- Stars
- 5.8k
- Forks
- 657
- Avg merge
- 20h 36m
- Merged PRs (30d)
- 6
Description
## Description
I am currently working on a proof of concept tool that could help practitioners migrate their Terraform HCL configurations from `null_resource` resource blocks to `terraform_data` resource blocks (as well as adding `moved` blocks). Renaming the block types in-place works great, however similar functionality is not available for [`hclwrite.Attribute`](https://pkg.go.dev/github.com/hashicorp/hcl/v2@v2.20.1/hclwrite#Attribute) to update the attribute name in-place. Currently the workaround seems something like:
```go
body.RemoveAttribute("triggers")
body.SetAttributeRaw("triggers_replace", triggersAttribute.Expr().BuildTokens(nil))
```
However that will cause the attribute and its expression to be moved to the bottom of the same block, causing unnecessary configuration churn.
## Proposal
Similar to the `(*hclwrite.Block).SetType()` method added in #340, create a new `(*hclwrite.Attribute).SetName()` method, e.g.
```go
func (*Attribute) SetName(name string)
```
That only replaces the underlying `name` node in the AST. I have verified this locally and will submit the small addition for consideration. Thank you!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at hclwrite.Attribute and compare the proposed API with (*hclwrite.Block).SetType(), which is cited as the precedent. Done means Attribute supports SetName while preserving the attribute and expression in their existing position instead of recreating them at the bottom of the block.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100