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 ¶
- func Fill(b Builtin, f Family, given map[string]string) (string, error)
- func LongestValue(v Var) string
- func Mutating(b Builtin) bool
- func Parse(b Builtin, f Family, o Output) (res any, perr *out.Error)
- func ParseShape(s string) (string, int, error)
- func RefuseNoSudo(b Builtin, noSudo bool) error
- func RunClient(ctx context.Context, b Builtin, c Client, given map[string]string) (any, error)
- func SelfElevates(b Builtin) bool
- func TypeUnsupported(b Builtin, target, typ string) *out.Error
- func Unsupported(b Builtin, family string) *out.Error
- type Builtin
- type Channel
- type Client
- type Data
- type Family
- type Output
- type Registry
- type Values
- type Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fill ¶
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 ¶
LongestValue is the longest value v takes: for a list, MaxItems longest values.
func Mutating ¶
Mutating reports whether the snippet changes something: the guard then asks on a protected entry, as the replaced command does.
func Parse ¶
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 ¶
ParseShape splits "svc-list/1" into its name and major.
func RefuseNoSudo ¶
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 ¶
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 ¶
SelfElevates reports whether the snippet's Unix text raises itself with sudo (`exec sudo -n sh -c '...'`), as the package reads do.
func TypeUnsupported ¶
TypeUnsupported is the error for a target of another entry type.
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).
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 ¶
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.
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.