Documentation
¶
Overview ¶
Package hiera is a pure-Go (CGO-free) reimplementation of Puppet's Hiera 5 hierarchical data-lookup engine.
It loads a Hiera 5 configuration (hiera.yaml), walks the configured hierarchy of data sources, and resolves a key to a typed value, honouring Hiera's merge behaviours (first / unique / hash / deep), per-key lookup_options, dotted-key digging into structured data, and the full %{...} interpolation grammar (scope / facts / trusted variables plus the scope, lookup, hiera, alias, literal functions) with interpolation-loop detection. It is faithful to Hiera 5 semantics so it can back Puppet's lookup() function.
Composition ¶
The engine does not resolve fact/variable references itself: the caller supplies a Scope (a pluggable variable provider). This is the seam a facts engine such as go-facter, or the Ruby binding go-ruby-hiera, plugs into — %{facts.os.name}, %{trusted.certname} and %{scope('x')} are resolved against the injected Scope. The engine therefore has no hard dependency on any particular fact source.
Value model ¶
Data files are decoded to a small, fixed set of Go types so a host can map results onto its own object graph:
Hash -> map[string]any Array -> []any String -> string Integer -> int64 Float -> float64 Boolean -> bool Null -> nil
YAML is decoded through github.com/go-ruby-yaml/yaml (the ecosystem's Ruby-faithful, pure-Go Psych backend, matching the YAML semantics Puppet relies on); JSON through the standard library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Version int
Defaults Defaults
Hierarchy []HierarchyEntry
// contains filtered or unexported fields
}
Config is a parsed Hiera 5 configuration (hiera.yaml).
func ParseConfig ¶
ParseConfig parses hiera.yaml bytes; dir is the directory containing it.
type Defaults ¶
type Defaults struct {
DataDir string
BackendKind string // "", "data_hash", "data_dig", "lookup_key"
BackendName string
Options map[string]any
}
Defaults holds the config-level defaults inherited by hierarchy levels.
type Hiera ¶
type Hiera struct {
// contains filtered or unexported fields
}
Hiera is a configured lookup engine: a parsed configuration bound to a Scope and a set of data-source backends.
func Load ¶
Load reads and parses the hiera.yaml at configPath and returns an engine resolving interpolation against scope. The configuration's directory is the base for resolving relative datadirs.
func New ¶
New returns a lookup engine for cfg resolving interpolation against scope. The built-in yaml_data and json_data backends are registered; register more with Hiera.RegisterDataHash. New panics if cfg or scope is nil.
func (*Hiera) Lookup ¶
Lookup resolves key and returns its value, whether it was found, and any error. key may be dotted (e.g. "profile.ntp.servers.0") to dig into structured data after the root key is looked up and merged.
func (*Hiera) RegisterDataHash ¶
func (h *Hiera) RegisterDataHash(name string, fn func(data []byte, path string) (map[string]any, error))
RegisterDataHash registers (or replaces) a data_hash backend under name so a hierarchy level may select it via "data_hash: name". A backend parses the raw bytes of one data file into a hash; path is supplied for error context.
type HierarchyEntry ¶
type HierarchyEntry struct {
Name string
Paths []string // from path / paths, relative to the datadir
Globs []string // from glob / globs, relative to the datadir
MappedPaths []string // exactly 3 elements: [scope_key, loop_var, template]
DataDir string
BackendKind string // "", "data_hash", "data_dig", "lookup_key"
BackendName string
Options map[string]any
}
HierarchyEntry is one level of the hierarchy.
type MapScope ¶
MapScope is a Scope backed by a nested map of variables. A dotted reference digs through nested map[string]any and []any (numeric index) values, so facts such as {"os": {"name": "Debian"}} resolve for "facts.os.name" when stored under the "facts" key. A leading "::" is ignored, matching Puppet's top-scope syntax.
type MergeKind ¶
type MergeKind int
MergeKind is a Hiera merge behaviour.
const ( // MergeFirst returns the highest-priority value found (the default). MergeFirst MergeKind = iota // MergeUnique flattens every found array/scalar into a single // deduplicated array. MergeUnique // MergeHash shallow-merges every found hash, higher priority winning. MergeHash // MergeDeep recursively merges every found hash, higher priority winning. MergeDeep )
type MergeStrategy ¶
type MergeStrategy struct {
Kind MergeKind
KnockoutPrefix string // deep: a prefixed array element removes the plain one
MergeHashArrays bool // deep: merge arrays element-wise instead of unioning
SortMergedArrays bool // unique/deep: sort merged arrays
}
MergeStrategy selects a merge behaviour and its options.
type Options ¶
type Options struct {
// Merge, when non-nil, overrides the merge behaviour for this call.
Merge *MergeStrategy
// Default supplies a default_value returned when the key is not found.
Default Value
// HasDefault enables Default (so a nil default value is distinguishable).
HasDefault bool
// DefaultValuesHash supplies per-key defaults consulted, by the looked-up
// root key, when the key is not found and HasDefault is false.
DefaultValuesHash map[string]any
}
Options controls a single Hiera.Lookup call. A nil *Options is equivalent to the zero value: the merge behaviour comes from lookup_options (falling back to "first") and there is no default.
type Scope ¶
Scope is the pluggable variable provider the engine resolves interpolation against. Lookup receives the raw reference used inside %{...} or as the argument to scope() — a bare name ("certname") or a dotted path ("facts.os.name", "trusted.certname") — and returns the value and whether it is present. This is the seam a facts engine (go-facter) or the Ruby binding (go-ruby-hiera) implements to feed facts / trusted / server_facts data in.
