amplify-education / python-hcl2

MIT License
246 stars 56 forks source link

Support python hcl2 object to hcl2 text #23

Open jordiclariana opened 4 years ago

jordiclariana commented 4 years ago

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
danieladams456 commented 4 years ago

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

vikas027 commented 4 years ago

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.

ChristophShyper commented 4 years ago

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 :)

antonbabenko commented 4 years ago

+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).

tr-fernandocunha commented 2 years ago

+1

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

thereforsunrise commented 7 months ago

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

swarner1033 commented 2 months ago

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

kvendingoldo commented 1 month ago

any news here? :)

cah-david-schaffner commented 1 month ago

any news here?