hairyhenderson/gomplate

[Feature] something like `{{-` and `-}}` but that trims newlines only

ItalyPaleAle opened this issue · 4 comments

Consider this template:

      ports:
      {{ with (datasource "values").bind }}
        - containerPort: 80
          hostPort: 80
          hostIP: {{ .ip | toYAML }}
        - containerPort: 8081
          hostPort: 8081
          hostIP: {{ .ip | toYAML }}
      {{ end }}

When running, the output is:

      ports:
        
        # Expose HTTP port and API on the Tailscale interface only
        - containerPort: 80
          hostPort: 80
          hostIP: 127.0.0.1

        - containerPort: 8081
          hostPort: 8081
          hostIP: 127.0.0.1

        

I understand that I can use {{- and -}} to trim the whitespace. For example:

      ports:
      {{ with (datasource "values").bind -}}
        - containerPort: 80
          hostPort: 80
          hostIP: {{ .ip | toYAML -}}
        - containerPort: 8081
          hostPort: 8081
          hostIP: {{ .ip | toYAML -}}
      {{ end }}

However, that follows how Go templates work, and they remove any whitespace characters, not just newlines. So, the result is something similar to:

      ports:
        - containerPort: 80
          hostPort: 80
          hostIP: 127.0.0.1
- containerPort: 8081
          hostPort: 8081
          hostIP: 127.0.0.1

As you can see, this removes also the spaces before the next item in the array, so this breaks my YAML.

Is there a way gomplate could be configured to behave more like Helm in this specific case, where using {{- for example only removes until the previous newline?

more like Helm in this specific case, where using {{- for example only removes until the previous newline?

Interesting - if {{- behaves differently in Helm, I guess they've diverged from Go's text/template parsing?

I haven't used Helm in a few years, but from what I recall, it suffered the same issue. However, to get around that, I remember using Helm's nindent function.

A few ways you could work around this particular issue:

  1. output as JSON instead - it shouldn't (in theory?) have a trailing newline, and in this particular case the output will be valid in YAML as well: {{ .ip | toJSON }}
  2. remove the trailing newline that toYAML introduces - using strings.TrimSpace makes sense in this case: {{ .ip | toYAML | strings.TrimSpace }}
  3. use strings.Indent - {{ .ip | toYAML | strings.Indent 6 -}}

The behaviour of {{-/-}} is from Go's text/template package, and so its behaviour can't be changed for gomplate (at least, without diverging from the standard template language, which I don't want to do).

This issue is stale because it has been open for 60 days with no
activity. If it is no longer relevant or necessary, please close it.
Given no action, it will be closed in 14 days.

If it's still relevant, one of the following will remove the stale
marking:

  • A maintainer can add this issue to a milestone to indicate that
    it's been accepted and will be worked on
  • A maintainer can remove the stale label
  • Anyone can post an update or other comment

@ItalyPaleAle You can use trims at left only:

      ports:
      {{- with (datasource "values").bind }}
        - containerPort: 80
          hostPort: 80
          hostIP: {{ .ip | toYAML }}
        - containerPort: 8081
          hostPort: 8081
          hostIP: {{ .ip | toYAML }}
      {{- end }}

Output is:

      ports:
        - containerPort: 80
          hostPort: 80
          hostIP: {{ .ip | toYAML }}
        - containerPort: 8081
          hostPort: 8081
          hostIP: {{ .ip | toYAML }}

Thanks @xxxcoltxxx - I think that answers the issue. I'm going to close this now.