snippet

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 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 snippet is the engine of komando's ready-made snippets: a built-in, named read or change with one documented JSON shape (docs/design/code-layout.md). It holds the Builtin type, the checks of a definition, the registry, the filling of a text with quoted variables, the parse step and the JSON contract. It runs nothing itself: the command layer runs a filled text through the same guard and transport as a typed exec line, and hands the output back to Parse.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fill

func Fill(b Builtin, f Family, given map[string]string) (string, error)

Fill returns b's text for family f with each {{var:NAME}} replaced by the variable's value, quoted by that family's shell. A value is never pasted: it is always one quoted word. Each marker is replaced once, so a value that looks like a marker stays text.

func LongestValue

func LongestValue(v Var) string

LongestValue is the longest value v takes: for a list, MaxItems longest values.

func Mutating

func Mutating(b Builtin) bool

Mutating reports whether the snippet changes something: the guard then asks on a protected entry, as the replaced command does.

func Parse

func Parse(b Builtin, f Family, o Output) (res any, perr *out.Error)

Parse turns one target's output into the result. A parser's own coded error (*out.Error) is a check it carries from the replaced command and stays, with the rows it returns. Any other failure, a panic, or no result at all is SNIPPET_PARSE_FAILED with no result: never a partial or guessed shape, and never the raw text.

func ParseShape

func ParseShape(s string) (string, int, error)

ParseShape splits "svc-list/1" into its name and major.

func RefuseNoSudo

func RefuseNoSudo(b Builtin, noSudo bool) error

RefuseNoSudo is the error for --no-sudo on a snippet that raises itself (Audit A-457-1): the flag only drops the sudo the runner adds for Builtin.Sudo, so the run would elevate anyway. Nil when it is fine.

func RunClient

func RunClient(ctx context.Context, b Builtin, c Client, given map[string]string) (any, error)

RunClient runs a client snippet's call with c, the open client of its entry. It is the only way to reach Run: every variable is checked first (Check), so Run gets cleaned values and never a name or value the snippet did not declare. A text built-in has no client call and is refused.

func SelfElevates

func SelfElevates(b Builtin) bool

SelfElevates reports whether the snippet's Unix text raises itself with sudo (`exec sudo -n sh -c '...'`), as the package reads do.

func TypeUnsupported

func TypeUnsupported(b Builtin, target, typ string) *out.Error

TypeUnsupported is the error for a target of another entry type.

func Unsupported

func Unsupported(b Builtin, family string) *out.Error

Unsupported is the error for a family (or provider) the snippet has no body for.

Types

type Builtin

type Builtin struct {
	Name     string          // "svc-list"
	Type     string          // the entry type it runs against: host, database, cache, cluster
	Replaces string          // the command it replaces, as typed: "svc list"
	Reader   string          // the app screen or the written agent need (Audit A-C368-1)
	Tier     capability.Tier // static, never weaker than Replaces (rule 3.4)
	Shape    string          // "svc-list/1"
	Vars     []Var
	Checks   []string // the checks carried, each named by the test that pins it (Audit A-C368-4)
	// Sudo runs the Unix text with sudo, as the replaced command does;
	// --no-sudo turns it off. A Windows text never runs with sudo.
	Sudo  bool
	Text  map[Family]string
	Parse func(o Output, f Family) (any, error)
	Run   func(ctx context.Context, c Client, v Values) (any, error)
}

Builtin is one ready-made snippet. A text snippet has Text and Parse; a client snippet (SFTP, SQL, cache, Kubernetes API) has Run. Never both.

type Channel

type Channel uint8

Channel is where a variable's value goes, which decides how it is quoted (Audit A-C368-5).

const (
	Text     Channel // into the text: quoted by the family's shell (sshx.Q, sshx.PSQ)
	SQLIdent         // a SQL identifier: quoted by dbx for its engine, in the client call
	SQLValue         // a SQL value: a bind parameter, never spliced
	RESP             // a cache argument: its own RESP argument, never a joined line
	KubeName         // a Kubernetes name: DNS-1123, passed as an API field
	SFTPPath         // a remote path: cleaned, an SFTP argument, no shell
	Number           // a count into the text: ASCII digits only, written as they are
)

type Client

type Client any

Client is the open connection a client snippet works on: the command layer opens it for the entry's type (an SFTP session for a host, a read-only database, a cache store, a Kubernetes client), hands it to Run and closes it after. The engine only carries it; a type's built-ins read it as that type's own value, so the engine imports no transport.

type Data

type Data struct {
	Snippet  string     `json:"snippet"`
	Shape    string     `json:"shape"`
	Target   string     `json:"target"`
	OK       bool       `json:"ok"`
	Result   any        `json:"result,omitempty"`
	ExitCode *int       `json:"exit_code,omitempty"`
	Warnings []string   `json:"warnings,omitempty"`
	Error    *out.Error `json:"error,omitempty"`
}

Data is one target's answer: the JSON contract of `snippet exec` (design 5.1). Result is exactly what the replaced command put in a server's data. A failure has no result and no raw text, except the rows a carried check keeps (a unit not found still returns the units that were read).

func Decode

func Decode(raw []byte, want string) (Data, error)

Decode reads one target's answer for a reader that knows shape want (such as "svc-list/1"). It refuses another snippet or another major; fields it does not know are ignored, since an added field does not raise the major.

func NewData

func NewData(b Builtin, target string, result any, err *out.Error, exit *int, warnings []string) Data

NewData is one target's answer.

type Family

type Family string

Family is the shell a text is written for.

const (
	Unix    Family = "unix"    // POSIX sh: Linux, macOS
	Windows Family = "windows" // PowerShell
)

type Output

type Output struct {
	Stdout, Stderr string
	ExitCode       int
	Truncated      bool // stdout was cut at the output cap
}

Output is what a text printed on the server.

type Registry

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

Registry is the list of built-ins, looked up by exact name.

func New

func New(lists ...[]Builtin) (*Registry, error)

New checks every definition of the lists and returns the registry. The lists come from the type packages (hostcmd.Snippets(), ...), collected in one place: no init() registration.

func (*Registry) Lookup

func (r *Registry) Lookup(name string) (Builtin, bool)

Lookup finds a built-in by its exact name.

func (*Registry) Names

func (r *Registry) Names() []string

Names lists the built-ins, sorted.

type Values

type Values map[string]string

Values are a snippet's variables after Check, for a client call.

func Check

func Check(b Builtin, given map[string]string) (Values, error)

Check checks the given variables against b's and returns the values to use: defaults filled in, a path cleaned. It refuses a variable b does not declare, a missing required one, and a value that fails its checks.

type Var

type Var struct {
	Name    string
	Channel Channel
	Pattern *regexp.Regexp // anchored (^...$); the value must match it
	MaxLen  int            // bytes
	// Longest is a value of MaxLen bytes that matches Pattern: the filled
	// text with it must find no exec rule (Audit A-C368-4, rule 3.5)
	Longest  string
	Required bool
	Default  string // used when the variable is not given and not required
	// List takes several values, comma-separated: each is checked alone
	// and quoted as its own word, in the order given (svc-status takes
	// several units). MaxItems caps how many; MaxLen is per value.
	List     bool
	MaxItems int
	// Families narrows Pattern for one family's text, checked when that
	// text is filled: systemd unit names and Windows service names differ.
	Families map[Family]*regexp.Regexp
}

Var is one variable a snippet takes with --var NAME=VALUE.

Jump to

Keyboard shortcuts

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