hashicorp / hcl

HCL is the HashiCorp configuration language.
Mozilla Public License 2.0
5.27k stars 592 forks source link

heredoc doesn't seem to handle tabs #9

Closed dpetzel closed 8 years ago

dpetzel commented 10 years ago

After reviewing https://github.com/hashicorp/hcl/issues/6, I attempted to implement heredoc for user_data in terraform. I had added the following:

    user_data = <<-USER_DATA

    #cloud-config

    coreos:
      etcd:
        # multi-region and multi-cloud deployments need to use $public_ipv4
        addr: $private_ipv4:4001
        peer-addr: $private_ipv4:7001
      units:
        - name: etcd.service
          command: start
        - name: fleet.service
          command: start

    USER_DATA

This throws the following error: Error loading config: Error parsing /my_file.tf: Line 76, column 0: syntax error

It appears hcl is not honoring the - as outlined in http://en.wikipedia.org/wiki/Here_document:

Appending a minus sign to the << has the effect that leading tabs are ignored. This allows indenting here documents in shell scripts without changing their value.

If I unindent everything and remote the - it works OK, but IMO the file becomes much harder to read since all the heredoc data needs to be completely left-aligned.

mitchellh commented 10 years ago

This is correct, I've tagged as an enhancement.

You don't need to left-align all data. The - only affects the end marker, so you can remove it and put USER_DATA on the very left. Still not pretty, but it works.

In fact, I don't think - in any language ignores the leading tabs on the data in the heredoc, just the end marker. At the very least, Ruby is this way, but I tend to remember that being the case universally.

dpetzel commented 10 years ago

Indeed you are correct, the - seems to have no effect on that data in Ruby, it just allows you to not have to indent the end_marker. So in the case of instance user_data, I'm actually not sure this enhancement would even help then, since the I believe the user_data needs to not have the tabs (at least in the context of #cloud-config).

Phomias commented 8 years ago

In fact, I don't think - in any language ignores the leading tabs on the data in the heredoc, just the end marker. At the very least, Ruby is this way, but I tend to remember that being the case universally.

It's not quite universal. In BASH at least the leading tabs on the data are ignored when you use a - after the <<

Try it yourself. To type a tab use ctrl+v tab

$ cat <<-EOF
>       this
>       that
>       the other
>       EOF
this
that
the other
$ cat <<-EOF
>       this
>       that
>       the other
> EOF
this
that
the other
$ cat <<EOF
>       this
>       that
>       the other
> EOF
    this
    that
    the other

I'd love it if you aim for the bash-like behavior because there are times (like with json policy documents on aws) when you can't have leading whitespace. In fact, taking a page out of the YAML book, it would make me happiest (being a space-indenter) to have the leading (spaces or tabs) indent (of the first line) stripped from every line of the data.

Right now I have to use this:

resource "aws_iam_group_policy" "asset_managers_policy" {
  name =                        "asset_managers_policy"
  group = "${aws_iam_group.asset_managers.id}"
  policy = <<EOF
{
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "s3:*"
          ],
          "Resource": [
            "${aws_s3_bucket.assets_bucket.arn}"
          ]
        }
      ]
}
EOF
}

We can probably agree that everyone would rather read this:

resource "aws_iam_group_policy" "asset_managers_policy" {
  name =                        "asset_managers_policy"
  group = "${aws_iam_group.asset_managers.id}"
  policy = <<-EOF
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "s3:*"
          ],
          "Resource": [
            "${aws_s3_bucket.assets_bucket.arn}"
          ]
        }
      ]
    }
  EOF
}
mitchellh commented 8 years ago

@Phomias Correct, I'm talking about languages without a "-". I consider that additional syntax that we might add to HCL later but don't support currently.