You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there any plan to support going backwards (from dict to hcl2)?
Something like this:
importhcl2with(open("test.py", 'r')) asfile:
dict=hcl2.load(file)
# Now dict holds a python representation of the test.tf filehcl2_text=hcl2.dumps(dict)
# Now hcl2_text holds same content as test.py original file
Reacted by Aleksei Zhukov, Vikas Kumar, Nimrod Kor, Krzysztof Szyper, Anton Babenko, Denis Rampnoux, Michael Heyns, Aidan Melen, Vijay Bhaskar Reddy Avuthu, Zach Whaley and 27 more
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.
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 :)
Reacted by Jordi Clariana, Anton Babenko, Chris Slamar, tr-fernandocunha, Michael Lohr, xuanyuanaosheng, Jacek Ostański, Ari, Vikas Kumar and opensourcerfriday
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.
Reacted by Mark A. Hershberger, swarner1033, opensourcerfriday, kvendingoldo, Jay Pot and Nicolai Antiferov
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 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 :)
Reacted by opensourcerfriday, kvendingoldo and Jay Pot
Activity
danieladams456 commentedon Mar 3, 2020
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 commentedon Mar 3, 2020
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
tffiles tojsonjust one or two of those.ChristophShyper commentedon Mar 18, 2020
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 commentedon Mar 29, 2020
+1 for having this feature. It would be great to have this feature as you say, but from what I see here:
python-hcl2/hcl2/transformer.py
Lines 188 to 189 in 64a3d8b
The comments are simply ignored, so we can't really combine an updated file from the
dictobject.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 commentedon Dec 23, 2021
+1
In my case, I want to dynamically update a
tfvarsfile and create a PR using a custom bot.thereforsunrise commentedon Feb 5, 2024
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 commentedon Jul 23, 2024
This is my current work around https://nklya.medium.com/how-to-write-hcl2-from-python-53ac12e45874
kvendingoldo commentedon Aug 7, 2024
any news here? :)
cah-david-schaffner commentedon Aug 14, 2024
any news here?
weaversam8 commentedon Oct 2, 2024
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.
kkozik-amplify commentedon Oct 7, 2024
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!
weaversam8 commentedon Oct 7, 2024
@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 :)