hcl icon indicating copy to clipboard operation
hcl copied to clipboard

hclwrite: Block name and label

Open dkumor opened this issue 5 years ago • 3 comments

This issue is related to hashicorp/hcl2#23, in that I could not figure out the API in the hclwrite package to perform a modification within a specified existing block.

Suppose the following file:

attr1 = "foobar"
service "hello" {
   attr2 = "world"
}

I can modify attr1 by calling body.SetAttributeValue("attr1",...). I can also get the list of blocks in the file by calling body.Blocks().

I cannot, however find any way to tell that the block returned by Blocks() is of type "service" with the label "hello", which is where I want to modify attr2, if it exists - no such function or struct field seems to be publicly exposed.

I apologize if this is due to me missing some piece of documentation!

dkumor avatar Feb 16 '19 02:02 dkumor

Hi @dkumor! Thanks for reporting this.

The hclwrite API is currently not complete, as you've seen. We do plan to complete it in the future, allowing the sort of surgical update you want to perform here, but for the moment the priority is on making sure the parsing use-cases are solid. The hclwrite functionality we have is just the subset that was required to implement gohcl.EncodeAsBlock, which provides the (approximate) inverse of the higher-level decoder API in gohcl.

We'll use this issue to represent the work to implement this missing feature. However, unfortunately I expect we won't have the time to design and implement this in the very short term due to focus being elsewhere.

I expect the interface to this would be something like block.SetType("foo") and block.SetLabels([]string{"bar", "baz"), for consistency with body.SetAttributeValue, though as you saw we are also missing accessors block.GetType and block.GetLabels to read the existing values. I think (from a quick peek at the code) that the necessary internal structures are there to implement these (with the set operations replacing the typeName node and the labels node set respectively, though updating labels in-place looks a little trickier due to it being a sequence of tokens rather than a single token.

In general, the API for hclwrite is intended to follow similar principles to that of an XML DOM API, allowing properties of the source constructs to be accessed and modified both via accessor methods while retaining the relationships between items that are not modified.

apparentlymart avatar Feb 19 '19 16:02 apparentlymart

Do we have an update on this by chance? I noticed the v2 label was added and that the hcl2 version on godoc is also out.

clarkmcc avatar Dec 15 '19 05:12 clarkmcc

Thanks to hashicorp/hcl2#126, which we merged just before the v2.0.0 release, there are now methods to read the type and labels from a block.

There is not yet a facility to set the type or the labels as I described in my earlier comment. I am not personally working on that right now because my focus is currently elsewhere, but I'd love to review a PR for it if someone else has the time and motivation to work on it, with the methods I described in my earlier comment.

apparentlymart avatar Dec 15 '19 19:12 apparentlymart