gotmpl Transform

Execute a Go template with Sprig functions. The first argument is the template file, subsequent arguments are optional data files that are merged and available as .Data.

Usage

node:
  template:
    from_file: config.gotmpl
  vars:
    from_file: variables.yaml
  config:
    to_file: config.json
    transform:
      gotmpl:
        - "{{.meta.template.path}}"
        - "{{.meta.vars.path}}"

Template Context

Templates have access to:

Variable Description
.nodeName Parsed node data (direct access to fields)
.meta.nodeName.path Node file path
.meta.nodeName.format Node file format
.Data Merged data from data files (gotmpl args)
env "VAR" Environment variable (Sprig)
toYaml Convert value to YAML string
toToml Convert value to TOML string
All Sprig functions See Sprig docs

Input Files

Template files (.gotmpl, .tmpl) are loaded as path-only inputs - their content is not parsed as data. This allows you to reference them in the transform while keeping the template content separate.

Examples

Basic Template

config.gotmpl:

{ "greeting": "Hello, World!" }

panconf.yaml:

node:
  template:
    from_file: config.gotmpl
  config:
    to_file: config.json
    transform:
      gotmpl:
        - "{{.meta.template.path}}"

With Data File

variables.yaml:

database:
  host: localhost
  port: 5432
debug: true

config.gotmpl:

{ "database": "{{ .Data.database.host }}:{{ .Data.database.port }}", "debug": {{ .Data.debug }} }

Result:

{
  "database": "localhost:5432",
  "debug": true
}

Multiple Data Files

Data files are deep-merged in order:

transform:
  gotmpl:
    - "{{.meta.template.path}}"
    - "{{.meta.defaults.path}}"
    - "{{.meta.environment.path}}"

Using Sprig Functions

config.gotmpl:

{ "uppercase": "{{ .Data.name | upper }}", "default": "{{ .Data.missing | default "fallback" }}", "joined": "{{ .Data.items | join "," }}", "encoded": {{ .Data.settings | toJson }}, "home": "{{ env "HOME" }}", "now": "{{ now | date "2006-01-02" }}" }

Accessing Node Data Directly

Access parsed data from any node using .nodeName.field:

config.gotmpl:

{ "name": "{{ .config.name }}", "version": "{{ .config.version }}" }

Iterating Over Glob Data

Use .items to access individual items from a glob input:

config.gotmpl:

Items: {{- range .items.items }} - {{ .name }}: {{ .value }} {{- end }}

Accessing Node Metadata

config.gotmpl:

# Generated from: {{ .meta.vars.path }} # Format: {{ .meta.vars.format }} config: source: "{{ .meta.vars.path }}"

Using toYaml and toToml

Embed structured data as YAML or TOML strings:

config.gotmpl:

database: {{ .db | toYaml | nindent 2 }}

Template Functions

Panconf Functions

Function Example Description
toYaml {{ .config | toYaml }} Convert value to YAML string
toToml {{ .config | toToml }} Convert value to TOML string

Common Sprig Functions

Function Example Description
default {{ .val | default "x" }} Default value if empty
env {{ env "HOME" }} Environment variable
toJson {{ .obj | toJson }} Encode as JSON
nindent {{ .obj | toYaml | nindent 2 }} Newline + indent
indent {{ .obj | toYaml | indent 2 }} Indent without newline
upper {{ .s | upper }} Uppercase
lower {{ .s | lower }} Lowercase
trim {{ .s | trim }} Trim whitespace
join {{ .list | join "," }} Join list
split {{ .s | split "," }} Split string
b64enc {{ .s | b64enc }} Base64 encode
b64dec {{ .s | b64dec }} Base64 decode
now {{ now }} Current time
date {{ now | date "2006-01-02" }} Format date

See Sprig documentation for the complete list.

Output Format

The template output must be valid YAML or JSON. This is because panconf parses the output to enable:

  1. Re-encoding to different formats (JSON, YAML, TOML)
  2. Using the output as input for other nodes via {{.nodeName}}

See Also