appendEnvVarLine appends a YAML env var entry to lines. If the value contains embedded newlines (e.g. from a multi-line YAML block scalar like >- with extra-indented continuation lines), it is emitted as a YAML literal block scalar (|) with proper indentation. At most one trailing newline (produced
(lines []string, key, value string)
| 313 | // by block scalars) is trimmed before processing; multiple intentional trailing |
| 314 | // newlines are preserved. |
| 315 | func appendEnvVarLine(lines []string, key, value string) []string { |
| 316 | // Trim at most one trailing newline added by YAML | or > block scalars. |
| 317 | // Using TrimSuffix (not TrimRight) to avoid stripping multiple trailing |
| 318 | // newlines that may be intentional in the value. |
| 319 | value = strings.TrimSuffix(value, "\n") |
| 320 | |
| 321 | if !strings.Contains(value, "\n") { |
| 322 | // Single-line: emit inline with YAML-safe quoting |
| 323 | return append(lines, fmt.Sprintf(" %s: %s", key, yamlStringValue(value))) |
| 324 | } |
| 325 | |
| 326 | // Multi-line: emit as a literal block scalar so embedded newlines are preserved |
| 327 | lines = append(lines, fmt.Sprintf(" %s: |", key)) |
| 328 | for line := range strings.SplitSeq(value, "\n") { |
| 329 | lines = append(lines, " "+line) |
| 330 | } |
| 331 | return lines |
| 332 | } |
| 333 | |
| 334 | // yamlStringValue returns a YAML-safe representation of a string value. |
| 335 | // If the value starts with a YAML flow indicator ('{' or '[') or other characters |