python-hcl2 icon indicating copy to clipboard operation
python-hcl2 copied to clipboard

Support python hcl2 object to hcl2 text

Open jordiclariana opened this issue 5 years ago • 9 comments

Is there any plan to support going backwards (from dict to hcl2)? Something like this:

import hcl2
with(open("test.py", 'r')) as file:
    dict = hcl2.load(file)
# Now dict holds a python representation of the test.tf file

hcl2_text = hcl2.dumps(dict)
# Now hcl2_text holds same content as test.py original file

jordiclariana avatar Jan 17 '20 16:01 jordiclariana

I think the terraform json syntax is preferred for machine generated code. That would be much easier to implement than going back to HCL2.

danieladams456 avatar Mar 03 '20 21:03 danieladams456

hey @danieladams456 , that is true but it is still a good idea to have the option to convert the dict back to hcl2. In my use case, I am not converting all my tf files to json just one or two of those.

vikas027 avatar Mar 03 '20 23:03 vikas027

It would be great to have possibility to write HCL back to a file. Imagine one wants to parse something during CI/CD phase and commit it back to GitHub after, while still having neat HCL code. Not a nasty JSON :)

ChristophShyper avatar Mar 18 '20 07:03 ChristophShyper

+1 for having this feature. It would be great to have this feature as you say, but from what I see here:

https://github.com/amplify-education/python-hcl2/blob/64a3d8b1e85d4521b041bbc36bd98e7dfa2be812/hcl2/transformer.py#L188-L189

The comments are simply ignored, so we can't really combine an updated file from the dict object.

The only solution which can do correct manipulation is the official Go library - https://github.com/hashicorp/hcl/tree/hcl2 .

Please correct me if I am wrong and you know another solution (preferably in Python).

antonbabenko avatar Mar 29 '20 20:03 antonbabenko

+1

In my case, I want to dynamically update a tfvars file and create a PR using a custom bot.

tr-fernandocunha avatar Dec 23 '21 22:12 tr-fernandocunha

ChatGPT brought me here. It keeps suggesting I use hcldumps to convert the json back to HCL - no doubt because of this thread. :D

thereforsunrise avatar Feb 05 '24 10:02 thereforsunrise

This is my current work around https://nklya.medium.com/how-to-write-hcl2-from-python-53ac12e45874

swarner1033 avatar Jul 23 '24 02:07 swarner1033

any news here? :)

kvendingoldo avatar Aug 07 '24 19:08 kvendingoldo

any news here?

cah-david-schaffner avatar Aug 14 '24 17:08 cah-david-schaffner

Hi everyone! I've implemented a big first step in this direction in my PR #169. While this wouldn't allow you to directly convert a dictionary back to HCL, it does allow you to manipulate an AST (produced by this library) in Python code and convert that back to HCL. See my PR for more details.

weaversam8 avatar Oct 02 '24 19:10 weaversam8

Hey everyone. Following up on @weaversam8, we just released #169 under version 5.0.0. Furthermore, we will keep looking into supporting reconstruction of JSON/dict to HCL2 text. Thanks!

kkozik-amplify avatar Oct 07 '24 15:10 kkozik-amplify

@kkozik-amplify I have an experimental branch in the works for going from dict to HCL2 as well - I will share a PR (or draft PR) for that when I make a bit more progress :)

weaversam8 avatar Oct 07 '24 19:10 weaversam8