-
Notifications
You must be signed in to change notification settings - Fork 599
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
Feature Request: Rename labels of block with hclwrite #338
Comments
Hi @minamijoyo! Thanks for starting this discussion, and sharing your use-case. After reviewing the current
If I recall correctly, the second two of these were added by you in an earlier PR, right? I think the most natural way to extend this API is with two setter methods corresponding to the above getter methods:
So far the precedent has been that we always manipulate all of the labels together, so I think it makes sense to continue that here and require resetting all of the labels in a single call, rather than a more complicated interface for setting individual labels. A caller that wants to update one label out of a set could use labels := block.Labels()
labels[1] = "new_name"
block.SetLabels(labels) An advantage of this approach is that we don't need to think about special rules to handle situations where the caller tries to set a new label that doesn't already exist, or have a separate method for removing a particular label while retaining the others, etc. Instead, the caller can just manipulate the labels slice using normal Go code and then set it back. I notice in your input you've used the alternative syntax of unquoted identifiers as labels. Unfortunately, I'm going to ask that we make Does the above API proposal seem like it would meet your needs? I've not looked in the code yet to see what the implementation complexity of this might be, but since the label tokens are already separated in order to support the Thanks again for sharing this use-case, and for the offer to implement it! |
Hi @apparentlymart, Thank you for your quick reply.
Yeah, I'm glad to hear that you remember my PR 😉
It looks natural and good to me. I'll try to implement it.
It's ok to force to use quoted labels. I didn't know the fact that unquoted labels are old syntax. No problems. Thanks again! |
Fixes hashicorp#338 Add methods to update block type and labels to enable us to easy refactor HCL configurations. - `*Block.SetType(typeName string)` - `*Block.SetLabels(labels []string)` For SetLabels, since we cannot assume that old and new labels are equal in length, remove old labels and insert new ones before TokenOBrace. To implement this, I also added the following methods. - `*nodes.Insert(pos *node, c nodeContent) *node` - `*nodes.InsertNode(pos *node, n *node) *node` They are similar to the existing Append / AppendNode, but insert a node before a given position.
Fixes hashicorp#338 Add methods to update block type and labels to enable us to refactor HCL configurations such as renaming Terraform resources. - `*Block.SetType(typeName string)` - `*Block.SetLabels(labels []string)` Some additional notes about SetLabels: Since we cannot assume that old and new labels are equal in length, remove old labels and insert new ones before TokenOBrace. To implement this, I also added the following methods. - `*nodes.Insert(pos *node, c nodeContent) *node` - `*nodes.InsertNode(pos *node, n *node) *node` They are similar to the existing Append / AppendNode, but insert a node before a given position.
@apparentlymart Done. I've just submitted #340. Please check it. Thanks! |
Fixes hashicorp#338 Add methods to update block type and labels to enable us to refactor HCL configurations such as renaming Terraform resources. - `*Block.SetType(typeName string)` - `*Block.SetLabels(labels []string)` Some additional notes about SetLabels: Since we cannot assume that old and new labels are equal in length, remove old labels and insert new ones before TokenOBrace. To implement this, I also added the following methods. - `*nodes.Insert(pos *node, c nodeContent) *node` - `*nodes.InsertNode(pos *node, n *node) *node` They are similar to the existing Append / AppendNode, but insert a node before a given position.
Fixes #338 Add methods to update block type and labels to enable us to refactor HCL configurations such as renaming Terraform resources. - `*Block.SetType(typeName string)` - `*Block.SetLabels(labels []string)` Some additional notes about SetLabels: Since we cannot assume that old and new labels are equal in length, remove old labels and insert new ones before TokenOBrace. To implement this, I also added the following methods. - `*nodes.Insert(pos *node, c nodeContent) *node` - `*nodes.InsertNode(pos *node, n *node) *node` They are similar to the existing Append / AppendNode, but insert a node before a given position.
Fixed in #340. For those interested in this, you can play with my proof of concept project. https://github.com/minamijoyo/hcledit Given the following hcl configuration:
You can rename the label:
|
I'm refactoring my Terraform configuration. The code has approximately 30K+ lines, so it's hard to do by hand and I looking for a way to writing automation scripts using hclwrite package in v2.
There are several operations required for refactoring, but I noticed that labels of block could not be updated. The current implementation does not have public API to set labels, so I tried to create a new block and replace it, but no luck.
The following is an example code simplified my attempts to clarify what expected and got. The result obviously has a lot of problems.
The result contains the following problems:
In my cases, 3 and 4 are not serious problems.
I think replacing the existing block with a new one looks bad way.
Is it acceptable to add an API to update labels of block?
If so, what interface should be?
Once the design are agreed, I will try to implement it.
The text was updated successfully, but these errors were encountered: