Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hclwrite printing maps over multiple lines #356

Closed
rifelpet opened this issue Mar 23, 2020 · 3 comments
Closed

hclwrite printing maps over multiple lines #356

rifelpet opened this issue Mar 23, 2020 · 3 comments

Comments

@rifelpet
Copy link

rifelpet commented Mar 23, 2020

I'm using hclwrite to print some Terraform resource arguments that are maps. I'm having trouble getting them to span multiple lines using the higher-level hclwrite functions. Similar to #347, I'm wondering if this can be achieved with SetAttributeValue or if I'll need to resort to SetAttributeRaw.

This gist shows my three attempts:

  1. The first uses SetAttributeValue on the entire map value. This is the exact syntax I'm looking for except it is all on one line rather than separate lines per key-value pair.
  2. The second uses AppendNewBlock and separate SetAttributeValues per pair. This is missing the equals sign as well as quoting around the first key, causing it to be treated as a division expression.
  3. The third uses SetAttributeRaw on the entire contents of the map. This outputs what I want, I'm just wondering if theres a simpler way.

HCL Template

The generated HCL from the above gist:

resource "aws_elb" "foo1" {
  tags = { "f.o/o" = "bar", foo = "bar" }
}
resource "aws_elb" "foo2" {
  tags {
    f.o / o = "bar"
    foo     = "bar"
  }
}
resource "aws_elb" "foo3" {
  tags = {
    "f.o/o" = "bar"
    "foo"   = "bar"
  }
}

The desired HCL:

resource "aws_elb" "foo3" {
  tags = {
    "f.o/o" = "foobar"
    foo     = "bar"
  }
}

As mentioned, this can be achieved with my third attempt in the gist plus some extra logic around optionally quoting the keys which would be trivial to implement.

Is there an easier way to achieve this with hclwrite or should I continue with SetAttributeRaw or perhaps some other utility function like hclwrite.Format? Any input would be greatly appreciated. Thanks!

@leakingtapan
Copy link

I have similar problem but for keep multiline format for list element. I want to append a new element to an existing hcl list programmatically. Any recommendations for how to achieve this?

@rifelpet
Copy link
Author

@apparentlymart I'm sure you're busy but when you get a chance, do you any thoughts on this?

@alisdair
Copy link
Member

@rifelpet We recently changed the behaviour of hclwrite in #372 to always generate multiple lines for non-empty maps and objects. This was released in v2.5.0.

I'm closing this issue as I think it's addressed, but feel free to reopen if I'm mistaken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants