template

package
v0.78.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package template provides functions for applying go-templates within Zarf.

Package template provides functions for applying go-templates within Zarf.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, s string, objs Objects) (string, error)

Apply takes a string, fills in the templates with the given Objects, and returns a new string.

func ApplyToFile

func ApplyToFile(ctx context.Context, src, dst string, objs Objects) error

ApplyToFile load a file path at src, fills in the templates with the given Objects, then writes the file to dst.

Types

type Objects

type Objects map[string]any

Objects provides a map of arbitrary data to be used in the template. By convention, top level keys are capitalized so users can see what fields are set by the system and which are set by user input. Example: Within a template, a user can access the Values from Object{ "Values": { "app": { "name": "foo" }}} With {{ .Values.app.name }} => "foo"

func NewObjects

func NewObjects(values value.Values) Objects

NewObjects instantiates an Objects map, which provides templating context. The "with" options below allow for additional template Objects to be included.

func (Objects) WithBuild

func (o Objects) WithBuild(build v1alpha1.ZarfBuildData) Objects

WithBuild takes the v1alpha1.ZarfBuildData section of a created package and makes it available in templating Objects.

func (Objects) WithConstants

func (o Objects) WithConstants(constants []v1alpha1.Constant) Objects

WithConstants Takes a slice of v1alpha1.Constants and unwraps it into the templating Objects map so constants can be accessed in templates by their key name.

func (Objects) WithMetadata

func (o Objects) WithMetadata(meta v1alpha1.ZarfMetadata) Objects

WithMetadata takes the v1alpha1.ZarfMetadata section of a created package and makes it available in templating Objects.

func (Objects) WithPackage

func (o Objects) WithPackage(pkg v1alpha1.ZarfPackage) Objects

WithPackage takes a v1alpha1.ZarfPackage and makes Metadata, Constants, and Build available on the Objects map.

func (Objects) WithState added in v0.78.0

func (o Objects) WithState(access StateAccess) (Objects, error)

WithState adds Zarf runtime state to the template Objects under the "State" key. Non-sensitive fields (addresses, usernames, counts) are always included. Sensitive field groups are only included when the corresponding StateAccessKey is present in access.AccessKeys — accessing an absent group causes a template error (missingkey=error). If access.State is nil, the Objects map is returned unchanged.

func (Objects) WithValues

func (o Objects) WithValues(values value.Values) Objects

WithValues takes a value.Values and makes it available in templating Objects.

func (Objects) WithVariables

func (o Objects) WithVariables(vars variables.SetVariableMap) Objects

WithVariables takes a variables.SetVariableMap and unwraps it into the templating Objects map so variables can be accessed by their key name.

type Refs added in v0.78.0

type Refs struct {
	Values [][]string
}

Refs are the system-object references discovered in a template string. Each Values entry is the slice of path segments following ".Values" (e.g. ".Values.db.host" -> {"db", "host"}).

func ReferencedKeys added in v0.78.0

func ReferencedKeys(s string) (Refs, error)

ReferencedKeys parses s as a go-template and returns the .Values references it makes. It does not execute the template. Direct field access (.Values.db.host) and index access with string literals (index .Values "db" "host") are resolved. References that can only be resolved at execution time are not: a root dot rebound inside range/with, a dot stored in a variable ($v := .Values), or an index key that is not a string literal (index .Values $key). Those are silently ignored, so the result may undercount but never overcounts. Unparseable templates return an error.

type StateAccess added in v0.78.0

type StateAccess struct {
	// State is the Zarf runtime state loaded from the cluster.
	State *state.State
	// AccessKeys lists which groups of sensitive state fields the component may access.
	// Accessing a field whose group is not listed causes a template error (missingkey=error).
	AccessKeys []v1alpha1.StateAccessKey
}

StateAccess bundles the runtime state with the named access groups a component has declared. A zero value (nil State) is safe and causes WithState to be a no-op.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL