package facts

import "github.com/purpleidea/mgmt/lang/funcs/facts"

Package facts provides a framework for language values that change over time.

Index

Variables

var RegisteredFacts = make(map[string]func() Fact) // must initialize

RegisteredFacts is a global map of all possible facts which can be used. You should never touch this map directly. Use methods like Register instead.

Functions

func ModuleRegister

func ModuleRegister(module, name string, fn func() Fact)

ModuleRegister is exactly like Register, except that it registers within a named module. This is a helper function.

func Register

func Register(name string, fn func() Fact)

Register takes a fact and its name and makes it available for use. It is commonly called in the init() method of the fact at program startup. There is no matching Unregister function.

Types

type Fact

type Fact interface {
	String() string
	//Validate() error // currently not needed since no facts are internal
	Info() *Info
	Init(*Init) error
	Stream(context.Context) error

	// TODO: should we require this here? What about a CallableFact instead?
	Call(context.Context) (types.Value, error)
}

Fact is the interface that any valid fact must fulfill. It is very simple, but still event driven. Facts should attempt to only send values when they have changed. TODO: should we support a static version of this interface for facts that never change to avoid the overhead of the goroutine and channel listener? TODO: should we move this to the interface package?

type FactFunc

type FactFunc struct {
	*docsUtil.Metadata

	Fact Fact
}

FactFunc is a wrapper for the fact interface. It implements the fact interface in terms of Func to reduce the two down to a single mechanism.

func (*FactFunc) Call

func (obj *FactFunc) Call(ctx context.Context, _ []types.Value) (types.Value, error)

Call this fact and return the value if it is possible to do so at this time.

func (*FactFunc) Info

func (obj *FactFunc) Info() *interfaces.Info

Info returns some static info about itself.

func (*FactFunc) Init

func (obj *FactFunc) Init(init *interfaces.Init) error

Init runs some startup code for this fact.

func (*FactFunc) Stream

func (obj *FactFunc) Stream(ctx context.Context) error

Stream returns the changing values that this function has over time.

func (*FactFunc) String

func (obj *FactFunc) String() string

String returns a simple name for this function. This is needed so this struct can satisfy the pgraph.Vertex interface.

func (*FactFunc) Validate

func (obj *FactFunc) Validate() error

Validate makes sure we've built our struct properly.

type Info

type Info struct {
	Output *types.Type // output value type (must not change over time!)
	Err    error       // did this fact validate?
}

Info is a static representation of some information about the fact. It is used for static analysis and type checking. If you break this contract, you might cause a panic.

type Init

type Init struct {
	Hostname string // uuid for the host
	//Noop bool
	Output chan types.Value // Stream must close `output` chan
	World  engine.World
	Debug  bool
	Logf   func(format string, v ...interface{})
}

Init is the structure of values and references which is passed into all facts on initialization.

Source Files

facts.go func.go

Version
v0.0.0-20250322185616-c50a578426f1 (latest)
Published
Mar 22, 2025
Platform
linux/amd64
Imports
7 packages
Last checked
5 days ago

Tools for package owners.