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

Function similar to nindent but ignoring content's initial new line. #357

Open
ksa-real opened this issue Dec 17, 2022 · 0 comments
Open

Comments

@ksa-real
Copy link

ksa-real commented Dec 17, 2022

The ask is to have a function that acts like nindent, but skips the content's leading new line. The same effect is if the function act s as indent but doesn't indent the first line. This would allow templates to generate a leading new line that is useful in a lot of scenarios. The proposed name is snindent or sindent.

Explanation

A common way of using templates:

env:
  {{- include "my.multiline" . | nindent 2 }}
extra:
  field: abc

This assumes that "my.multiline" template produces a string without leading and trailing new lines. Let the template generate a list of "items" (line per item), each guarded by some condition.

{{- define "my.multiline" }}
{{- if condition1 }}
- line1
{{- end }}
{{- if condition2 }}
- line2
{{- end }}
{{- end }}

It is not possible to keep the two if blocks independent and keep no new lines around generated string for all possible values of boolean condition1 and condition2

So, the best for template is to generate a new line character per line: leading or trailing. If using trailing, then {{ include "sometemplate" . }} would generate likely unnecessary extra new line. {{ include "sometemplate" . -}} would in turn affect the next line indentation. So, a leading new line is the only option.

Workarounds

env:
  {{- include "my.multiline" . | trimPrefix "\n" | nindent 2 }}
ksa-real added a commit to ksa-real/grafana-oncall that referenced this issue Jun 22, 2023
- Enabling existing secrets for external MySQL and Redis
- Tolerate existing secrets for bundled charts.
- README.md: secrets handling explained.
- Fixed multiple bugs where missing required field was replaced with
  default instead of failing.
- PHONE_NOTIFICATIONS_LIMIT was on the wrong level: it was not set if
  existingSecret was true.

Next are the cosmetic changes. They improve chart consistency, e.g.
prevent generation of multiple new lines in certain cases:
- Common approach to spaces trimming. This typically allows curly blocks
  and actual strings indentation and nice `nindent` usage:
  - Two curly blocks should not trim the same space. I.e.
    "{{ ... -}} {{- ... }}" shouldn't happen.
  - Template generates either single line or multiline string. In both
    cases, no new line appears on both sides of the output string. So we
    delete unnecessary new lines inside and at the end of string with
    "trim-to-left" (`{{-` ) and the leading new line using
    "trim-to-right" (`-}}`).
    Note that trimming both leading and trailing new line is not
    always easily possible:
    Masterminds/sprig#357

    Example.

    ```
    {{- define "mytemplate" -}}
    {{ if someBoolean -}}
      {{ .Value.some }}
    {{- else -}}
      some string
    {{- end }}
    {{- end }}
    ```

- `template` replaced with `include`. It is often recommended to use
  `include` by default, as it allows pipelining.
github-merge-queue bot pushed a commit to grafana/oncall that referenced this issue Jun 23, 2023
- Enabling existing secrets for external MySQL and Redis
- Tolerate existing secrets for bundled charts.
- README.md: secrets handling explained.
- Fixed multiple bugs where missing required field was replaced with
default instead of failing.
- PHONE_NOTIFICATIONS_LIMIT was on the wrong level: it was not set if
existingSecret was true.

Next are the cosmetic changes. They improve chart consistency, e.g.
prevent generation of multiple new lines in certain cases:
- Common approach to spaces trimming. This typically allows curly blocks
and actual strings indentation and nice `nindent` usage:
- Two curly blocks should not trim the same space. I.e. "{{ ... -}} {{-
... }}" shouldn't happen.
- Template generates either single line or multiline string. In both
cases, no new line appears on both sides of the output string. So we
delete unnecessary new lines inside and at the end of string with
"trim-to-left" (`{{-` ) and the leading new line using "trim-to-right"
(`-}}`).
Note that trimming both leading and trailing new line is not always
easily possible: Masterminds/sprig#357

    Example.

    ```
    {{- define "mytemplate" -}}
    {{ if someBoolean -}}
      {{ .Value.some }}
    {{- else -}}
      some string
    {{- end }}
    {{- end }}
    ```

- `template` replaced with `include`. It is often recommended to use
`include` by default, as it allows pipelining.

## Checklist

- [ ] Tests updated - No tests for Helm chart
- [X] Documentation added
- [x] `CHANGELOG.md` updated

Co-authored-by: Ildar Iskhakov <Ildar.iskhakov@grafana.com>
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

1 participant