We're creating yaml using go-templates. In them, we have actions with multi-line output that need to be indented at specific indent level. We can use the indent
function for this, but it doesn't treat the first line differently and therefor requires the action definition have no indentation level.
For example:
foo:
bar:
baz:
{{ myYamlOutputtingAction | indent 6 }} # <-- notice 0 indent level
Is there a way I can place my action definitions at an indentation level that makes sense for the context of the template?
2.13.0+
Just nindent
instead of indent
. The sprig library includes this function for exactly this use case.
The same code from above can be written as:
foo:
bar:
baz:
{{ myYamlOutputtingAction | nindent 6 }}
2.13.0
You can change this:
foo:
bar:
baz:
{{ myYamlOutputtingAction | indent 6 }} # <-- notice 0 indent level
To this:
foo:
bar:
baz:
{{- "\n"}}{{ myYamlOutputtingAction | indent 6 }} # <-- properly indented with a little bit fluff
A little explanation
This works by ensuring whatever content follows the {{- "\n"}}
has 0 indentation. This means you can trade a hacky {{- "\n"}}
for proper indentation whenever it makes sense. We generally think it's worth it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With