Skip to content

Support python hcl2 object to hcl2 text #23

Closed

Description

@jordiclariana

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

Activity

  1. danieladams456 commented on Mar 3, 2020

    @danieladams456

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

  2. vikas027 commented on Mar 3, 2020

    @vikas027

    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.

  3. ChristophShyper commented on Mar 18, 2020

    @ChristophShyper

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

  4. antonbabenko commented on Mar 29, 2020

    @antonbabenko

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

    def new_line_or_comment(self, args: List) -> Discard:
    return Discard()

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

  5. tr-fernandocunha commented on Dec 23, 2021

    @tr-fernandocunha

    +1

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

  6. thereforsunrise commented on Feb 5, 2024

    @thereforsunrise

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

  7. swarner1033 commented on Jul 23, 2024

    @swarner1033
  8. kvendingoldo commented on Aug 7, 2024

    @kvendingoldo

    any news here? :)

  9. cah-david-schaffner commented on Aug 14, 2024

    @cah-david-schaffner

    any news here?

  10. weaversam8 commented on Oct 2, 2024

    @weaversam8
    Contributor

    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.

  11. kkozik-amplify commented on Oct 7, 2024

    @kkozik-amplify
    Collaborator

    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!

  12. weaversam8 commented on Oct 7, 2024

    @weaversam8
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions