render

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package render is the template engine behind the files sofcat writes for a placement and, later, the templates a placed tree ships (zip-items guide, "Templates for placement files"). Templates are Go text/template with missingkey=error and three functions: json, fact and cond. Facts come from pkg/conditions, never from pkg/facts, so makecatalogs can render the same templates against sample facts without the platform code.

Index

Constants

View Source
const (
	MaxTemplate = 256 << 10
	MaxOutput   = 1 << 20
	Timeout     = 5 * time.Second
)

Limits. A template over MaxTemplate does not parse; an output over MaxOutput, or a render past Timeout, fails.

Variables

This section is empty.

Functions

func Canonical

func Canonical(raw []byte) ([]byte, error)

Canonical re-encodes one JSON value: object keys sorted, two-space indent, numbers as written, a trailing newline and no HTML escaping, so the bytes do not depend on the template's whitespace. Anything that is not exactly one JSON value is an error.

func JSONValue

func JSONValue(v any) any

JSONValue converts an untyped YAML value for encoding/json, which cannot encode a map with non-string keys.

Types

type FileContext

type FileContext struct {
	// OS is windows or darwin.
	OS string
	// Scope is machine or user.
	Scope string
	// Placement is the placement the file belongs to: for a settings
	// placement, the settings placement itself.
	Placement Placement
	// Registers is the registered placement of a settings placement; nil
	// for a marketplace manifest.
	Registers *Registered
	// Items are the receipts of the placement the file describes (the
	// registered one for a settings placement), sorted by item.
	Items []Item
	// User is the login name in user scope; empty in machine scope.
	User string
}

FileContext is what a placement file template renders with (zip-items guide, "Templates for placement files"). Facts are reached through the fact and cond functions, not a field.

type Item

type Item struct {
	Name    string `json:"name"`
	Version string `json:"version"`
	// Managed is true when a manifest lists the item in managed_installs.
	Managed bool `json:"managed"`
	// Path is the tree's directory.
	Path string `json:"path"`
	// TreeHash is the catalog's hash of the archive's contents; PlacedHash
	// the hash of the tree as sofcat wrote it, which changes when a rendered
	// file does.
	TreeHash   string `json:"tree_hash"`
	PlacedHash string `json:"placed_hash"`
}

Item is one placed tree, from its receipt. The JSON names are the receipt's, so {{ json .Items }} reads like the receipts.

type Marketplace

type Marketplace struct {
	Name                                string
	Owner                               any
	Renames                             any
	AllowCrossMarketplaceDependenciesOn any
}

Marketplace is a marketplace block as harnesses.yaml holds it. Owner, Renames and AllowCrossMarketplaceDependenciesOn are the YAML values.

type Placement

type Placement struct {
	Name string
	// Root is the expanded placement root: the file's path for a settings
	// placement.
	Root string
	// Kind is directory, marketplace or settings.
	Kind string
	// Marketplace is the marketplace block, for a marketplace placement
	// only; nil otherwise.
	Marketplace *Marketplace
}

Placement describes a placement to a template.

type Registered

type Registered struct {
	Name string
	Root string
	Kind string
	// Marketplace is the registered placement's marketplace block; nil
	// unless it is a marketplace placement.
	Marketplace *Marketplace
}

Registered is the placement a settings placement registers.

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is a parsed template whose literal cond expressions passed conditions.Inputs, so the validator and the device reject the same ones.

func Parse

func Parse(name, source string) (*Template, error)

Parse parses source as the template errors call name. It rejects a source over MaxTemplate and a literal cond expression with a syntax error or a call to a time-dependent builtin such as now().

func (*Template) Render

func (t *Template) Render(data any, facts conditions.Facts, path string) ([]byte, conditions.Facts, error)

Render executes the template with data and facts and returns the output with the facts it read: every name passed to fact and every fact a cond expression named, with their values. path is where the output goes: one ending in .json must be JSON and comes back canonical (Canonical); any other is verbatim. An output that is empty or only whitespace is returned as nil, so a settings placement can decide to remove its file. The error names the template and, when the engine reports them, the line and column.

text/template has no cancellation, so a render that outlives the timeout is abandoned: its goroutine runs until its next write or fact or cond call, each of which fails once the timeout has passed. A template that loops without writing or calling a function runs on, which is the limit of what the engine can enforce.

type TreeContext

type TreeContext struct {
	OS        string
	Scope     string
	Placement Placement
	Item      TreeItem
	// User is the login name of the account the helper runs as, without
	// domain, in user scope; empty in machine scope.
	User string
}

TreeContext is what a template inside a placed tree renders with. It has no Items or Registers, and the item has no PlacedHash, which is the hash of the output.

type TreeItem

type TreeItem struct {
	Name     string `json:"name"`
	Version  string `json:"version"`
	Managed  bool   `json:"managed"`
	Path     string `json:"path"`
	TreeHash string `json:"tree_hash"`
}

TreeItem is the item whose tree is being rendered.

Jump to

Keyboard shortcuts

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