Skip to content

Latest commit

 

History

History
92 lines (69 loc) · 3.02 KB

File metadata and controls

92 lines (69 loc) · 3.02 KB

standalone

A plain Go text/template example (not Helm) that demonstrates seven template constructs and their CUE equivalents.

What this example demonstrates

Construct Template syntax CUE equivalent
Value reference {{ .Values.name }} #values.name
String formatting {{ printf "%s:%d" .Values.host .Values.port }} "\(#values.host):\(#values.port)"
Conditional (if/else) {{ if .Values.debug }}...{{ else }}...{{ end }} Two if guards with _nonzero
Scoped context (with) {{ with .Values.tls }}...{{ end }} if guard with rebinding
List iteration (range) {{ range .Values.features }}...{{ end }} for comprehension
Map iteration (range $k,$v) {{ range $key, $val := .Values.labels }}...{{ end }} for k, v in comprehension
Helper definition {{ define "fullname" }} + {{ template "fullname" . }} Hidden field _fullname + reference

The template and helpers

config.yaml.tmpl is a Go text/template that generates a server configuration in YAML. It uses all seven constructs listed above.

_helpers.tpl defines a fullname helper template that config.yaml.tmpl calls via {{ template "fullname" . }}.

Running the template with Go

execute.go is a self-contained Go program that reads data.yaml, parses both template files, and prints the rendered output:

cd examples/standalone
go run execute.go

Output:

server:
  name: myapp-server
  address: localhost:8080
  logLevel: debug
  tls:
    cert: /etc/ssl/cert.pem
    key: /etc/ssl/key.pem
  labels:
    app: myapp
    env: production
  features:
    - auth
    - metrics
    - tracing

The CUE equivalent

config.cue is the CUE output produced by helm2cue. It is generated automatically via go generate (see gen.go) and committed so you can browse it without running the tool.

To generate it manually:

helm2cue template examples/standalone/_helpers.tpl examples/standalone/config.yaml.tmpl > examples/standalone/config.cue

Key mappings in the generated CUE:

  • _fullname hidden field corresponds to {{ define "fullname" }}
  • _nonzero helper drives if/else and with guards
  • for comprehensions replace range loops

Evaluating the CUE

The same data.yaml works with both the Go template and the CUE file. To evaluate the CUE using the YAML data:

cue export examples/standalone/config.cue examples/standalone/data.yaml -l '#values:' --out yaml -e output[0]

This produces the same data as the Go program (key ordering may differ since YAML maps are unordered).

Note on .Values

The helm2cue template subcommand converts pure Go text/template files and expects the template context to use .Values.xxx field references. This is just a Go template field name — it does not imply any dependency on Helm. Helm/Sprig functions are not available in template mode (use chart mode for Helm charts).