hashicorp / hashicorp/hcl

[Question] hclwrite - Insert / InsertNode

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

Description

I closed my other open issue #581, in favour of this more concrete one.

Looking at the **hclwrite** functions [Insert](https://github.com/hashicorp/hcl/blob/a514df9c5e5a496ea3cfe6c41a4304b2398f6bb7/hclwrite/node.go#L133) and [InsertNode](https://github.com/hashicorp/hcl/blob/a514df9c5e5a496ea3cfe6c41a4304b2398f6bb7/hclwrite/node.go#L144), what do they expect as input for the `pos *node` argument?

An example would be great! I'm completely new to GO and I've been trying lots of stuff to the best of my abilities, but I struggle to being able to use the functions since I have no idea how to. However, being able to insert nodes at given positions is something that I'd really need to be able to do, so I'm really hoping for this functionality. 🙂

**For the sake of it, an excerpt from the relevant part of my closed issue/question:**
_But what I need to do, is to insert a block at a given position inside of a body (before/after block X), so kind of a InsertBlock() function if that makes sense.
I stumbled upon the [Insert](https://github.com/hashicorp/hcl/blob/c7ee8b78101c33b4dfed2641d78cf5e9651eabb8/hclwrite/node.go#L133) and [InsertNode](https://github.com/hashicorp/hcl/blob/c7ee8b78101c33b4dfed2641d78cf5e9651eabb8/hclwrite/node.go#L144) functions, and wonder if I can make use of them in some way to achieve what I need? If so, an example would be splendid! If not, what would be a suitable solution to achieve my needs?
How could I go on using the [Insert](https://github.com/hashicorp/hcl/blob/c7ee8b78101c33b4dfed2641d78cf5e9651eabb8/hclwrite/node.go#L133) and [InsertNode](https://github.com/hashicorp/hcl/blob/c7ee8b78101c33b4dfed2641d78cf5e9651eabb8/hclwrite/node.go#L144) functions from my code, like what would be expected for the first pos *node argument? For the c nodeContent it works fine for me passing a Block() to it (I guess, since it doesn't complain), but what should I pass to the pos *node argument? An example would be awesome!_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with hclwrite/node.go, especially the Insert and InsertNode definitions linked in the issue, and inspect how nodes and blocks are represented in nearby hclwrite code. Document what the pos *node argument must reference and show how to insert a block before or after an existing block; done means a newcomer can use the example to perform the requested insertion.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.