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 ¶
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 ¶
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 ¶
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.