Documentation
¶
Overview ¶
Package cmdcore defines a command once and serves it through both doors: the command line and an MCP tool. A command is a params struct P, a result struct R and a Run function. Tags on P give the flag, positional, JSON name, description and enum; R's JSON tags are the --json object and the MCP structured content; R's Text method is the human output and the MCP text.
Param tags: json:"name" (MCP), flag:"--name" and optional short:"-n" (CLI option), arg:"name" or arg:"name,optional" (CLI positional, in field order), desc:"...", mcpdesc:"..." (replaces desc in the MCP schema), enum:"a,b", required:"true" (required in the MCP schema), repeat:"true" (a []string given by repeating the flag), value:"number" (the placeholder for a flag's value in --help), min:"1" (the smallest whole number an integer field takes). Field types are string, bool, int, *int and []string.
Index ¶
- Constants
- func GlobalOptions(fr Frame) string
- func ListLine(synopsis, desc string, col int) string
- func NoCredentials(addr, path string) *cliframe.Error
- func NoEndpoint(kind NoEndpointKind, command string, addresses []string) *cliframe.Error
- func NoSuchEndpoint(addr string) *cliframe.Error
- func NotSignedIn(addr string) *cliframe.Error
- func ParentHelp(fr Frame, description string, usages []string) string
- func ParseAddress(s, hint string) (string, error)
- func ParsePrefix(s, hint string) (string, error)
- func RecipientSuspended(addr string) *cliframe.Error
- func RefusedMessage(n int) *cliframe.Error
- func Schema[P any]() map[string]any
- func ServerNotAnswering(url string) *cliframe.Error
- func StaticHelp(fr Frame, c StaticCommand) string
- func ToolError(err error) error
- type CLIFunc
- type Command
- type Detailer
- type Entry
- type Env
- type EnvFunc
- type ExitCoder
- type Frame
- type HelpOption
- type Hinter
- type NoEndpointKind
- type Opts
- type Registry
- func (r *Registry) Group(name string) string
- func (r *Registry) Groups(group string) []string
- func (r *Registry) HelpFor(name string, args []string) (string, bool)
- func (r *Registry) ListLine(name string, col int) string
- func (r *Registry) Lookup(name string) (Entry, bool)
- func (r *Registry) MCPTools() []mcp.Tool
- func (r *Registry) Names() []string
- func (r *Registry) RunCLI(name string, args []string, stdin io.Reader, stdout, stderr io.Writer, ...) int
- func (r *Registry) Tools() []Tool
- func (r *Registry) WithFrame(fr Frame) *Registry
- type Result
- type StaticCommand
- type Tool
Constants ¶
const DetailParamDescription = "Return the fuller object. Default false: the usual, minimal one."
DetailParamDescription is the description of the MCP param that asks for the fuller object.
const SessionNoEndpointHint = "start the session with MASH_ENDPOINT=<address>"
SessionNoEndpointHint is the hint of NoneForSession.
Variables ¶
This section is empty.
Functions ¶
func GlobalOptions ¶
GlobalOptions is the "Global options:" section of fr: the heading and the table (no trailing newline), "" when fr has none.
func ListLine ¶
ListLine is one entry of a tool's command list: two spaces, the synopsis, and the description from column col, wrapped to 80 columns with its continuation lines indented to col. When the synopsis reaches col the description starts on a line of its own.
func NoCredentials ¶
NoCredentials is the failure for an endpoint whose creds file is not there (exit 1). path is shown as the person knows it, with ~ for the home folder.
func NoEndpoint ¶
func NoEndpoint(kind NoEndpointKind, command string, addresses []string) *cliframe.Error
NoEndpoint is the one place the three wordings of having no endpoint are made. command is the command being run (the hint of SeveralInstalled names it: "mash send --endpoint acme.demo.alice"); addresses are the installed endpoints, for SeveralInstalled only.
func NoSuchEndpoint ¶
NoSuchEndpoint is the failure for an address nothing answers to (exit 1).
func NotSignedIn ¶
NotSignedIn is the failure for an endpoint whose credentials the server refused (exit 1).
func ParentHelp ¶
ParentHelp is the --help of a command that only holds others (mash key): its description, Usage with one line per command under it, and the Global options, in the same layout and width as a command's own.
func ParseAddress ¶
ParseAddress is the one check every command that takes an address makes. An address is owner.group.name: three dotted parts, each lower-case letters, digits and single dashes. It returns the address as it is shown (the same string). Anything else, the flat owner_group_name and the slashed owner/group/name included, is a usage error (exit 2): "<s> is not an address; write it as <the dotted form>", with the hint when it is not empty (the command's own help). When no dotted form can be made from s, the error says owner.group.name.
func ParsePrefix ¶
ParsePrefix is the check `mash who` makes of its prefix: an owner (acme), an owner and group (acme.demo) or a full address (acme.demo.bob), each part lower-case letters, digits and single dashes. acme.* and acme.demo.* are accepted and mean acme and acme.demo; there is no other pattern. It returns the prefix without the ".*". Anything else, the flat acme_demo and the slashed acme/demo included, is a usage error (exit 2): "<s> is not an owner, an owner.group or an address", as the specification words it.
func RecipientSuspended ¶
RecipientSuspended is the failure for a send to an endpoint that is suspended or was deleted: its inbox takes no mail (exit 1).
func RefusedMessage ¶
RefusedMessage is the failure for the number of a message serve refused because it was not verified (exit 1).
func ServerNotAnswering ¶
ServerNotAnswering is the failure for a server that could not be reached or did not answer (exit 1).
func StaticHelp ¶
func StaticHelp(fr Frame, c StaticCommand) string
StaticHelp is the --help of c in the layout of helpText: description, Usage, Examples, Arguments, Options and Global options, each after a blank line, no line wider than 80 columns.
Types ¶
type CLIFunc ¶
CLIFunc runs one command from its arguments (the global options already removed by cliframe.Extract) and returns the process exit code.
func CLI ¶
CLI adapts c to the command line.
Arguments are parsed from the params tags: positionals in field order, --flag value, --flag=value, short flags, bool flags, repeated flags for []string, and "--" to end the options. A bare "-" is a positional (standard input for --file, and for send's message). An unknown option, a missing value, a bad enum value, number or boolean, an empty required positional, a single-value option given twice and surplus positionals are usage errors (exit 2). On success the result's Text, or with --json its compact JSON, goes to stdout; a result that is an ExitCoder sets the exit code and one that is a Hinter adds a hint line on stderr.
type Command ¶
type Command[P any, R Result] struct { Name string // Group is the heading the tool's own help lists the command under (the specification's x-group): Everyday, Setup, // Integration or Raw SAP. Group string // NoMCP says the command has no MCP tool (the specification's x-mcp tool is null): a setup command an agent does // not hold. The command table's Tools and MCPTools leave it out. NoMCP bool Summary string MCPDescription string Description string Examples []string Run func(ctx context.Context, env *Env, p P) (R, error) }
Command is one command: its CLI name (the verb, also the MCP tool name), a one-line summary, the one-line description and the examples that --help prints, and its Run. MCPDescription is the MCP tool's description, written for the model; when empty the tool's description is the Summary. Group is the heading the tool's own help lists the command under.
type Detailer ¶
Detailer is implemented by a result that has more to say under the global detail option (cliframe.DetailFlag). In text the command line and the MCP tool print Text and then DetailText, the 'label: value' lines; with --json they print DetailValue, a fuller object that contains the usual one, in place of the result. A command whose result has no Detailer has nothing more to say, and the option changes nothing (a Text may still read Opts.Detail on its own). An MCP tool whose result is a Detailer takes the boolean param cliframe.DetailParam.
type Entry ¶
type Entry interface {
CLIName() string
Summary() string
// Group is the heading the tool's own help lists the command under.
Group() string
// HasMCP is whether the command is also an MCP tool.
HasMCP() bool
Tool(env EnvFunc) Tool
RunCLI(env EnvFunc, fr Frame, args []string, stdin io.Reader, stdout, stderr io.Writer, g cliframe.Global) int
// HelpFor is the --help text when args ask for it, and whether they do: it reads nothing and runs nothing.
HelpFor(fr Frame, args []string) (string, bool)
// Usage is the synopsis, "mash send <address> [<message>] [options]", and the one-line description.
Usage(tool string) (synopsis, description string)
}
Entry is one registered command, usable through both doors without its type parameters.
type Env ¶
type Env struct {
Svc any
Now func() time.Time
Opts Opts
// Stdin is the standard input of a CLI run, nil for an MCP call. StdinPiped says it is a pipe or file rather than a
// terminal.
Stdin io.Reader
StdinPiped bool
// StdinTimeout bounds a read of Stdin: a pipe that never writes would otherwise block the command for ever. Zero
// means no bound.
StdinTimeout time.Duration
// ReadFile reads a file for --file style options; nil means os.ReadFile.
ReadFile func(path string) ([]byte, error)
}
Env is what a command runs against. Svc is the command package's own service, asserted to the interface it needs, so tests can fake it.
type ExitCoder ¶
type ExitCoder interface {
ExitCode() int
}
ExitCoder is implemented by a result that carries the exit code of a report: status and doctor report on stdout and exit 1 when they found a problem: a problem is a result (exit 1 on the command line), not a failure to run. A result without it exits 0.
type Frame ¶
type Frame struct {
Tool string
Globals []HelpOption
}
Frame is what --help prints around a command: the tool's name and its global options.
type HelpOption ¶
type HelpOption struct {
Flag string // "--endpoint"
Short string // "-v", or ""
Value string // placeholder for the value, "address", or "" for a switch
Desc string
}
HelpOption is one global option as --help lists it.
type Hinter ¶
type Hinter interface {
Hint() string
}
Hinter is implemented by a result that carries a hint for a person: the command line prints it as "hint: HINT" on standard error (never with --json, whose object is the whole answer), and an MCP tool, which has no standard error, ends its text with the same line. An empty hint is no hint.
type NoEndpointKind ¶
type NoEndpointKind int
NoEndpointKind says where having no endpoint was found out, which decides its words (rules.json: "Having no endpoint reads the same in every mash command").
const ( // NoneInstalled is a shell with no endpoint chosen and none installed (exit 2). NoneInstalled NoEndpointKind = iota // SeveralInstalled is a shell with no endpoint chosen and several installed (exit 2). SeveralInstalled // NoneForSession is an MCP tool in a session that names no endpoint (exit 1). NoneForSession )
type Registry ¶
type Registry struct {
Entries []Entry
Env EnvFunc
Frame Frame // what --help prints around each command
}
Registry is one table of commands that both doors range over.
func NewRegistry ¶
NewRegistry returns a Registry of entries that make their Env with env.
func (*Registry) Group ¶
Group returns the heading of the command named name ("" when there is no such command).
func (*Registry) Groups ¶
Groups returns the names of the table's commands under heading group, in table order.
func (*Registry) HelpFor ¶
HelpFor is the --help text of the command named name when args ask for it.
func (*Registry) ListLine ¶
ListLine is the line the tool's own help gives the command named name: two spaces, its synopsis, and its one-line description starting at column col (on a line of its own, indented to col, when the synopsis is too long), wrapped to 80 columns. It is "" when there is no such command.
func (*Registry) RunCLI ¶
func (r *Registry) RunCLI(name string, args []string, stdin io.Reader, stdout, stderr io.Writer, g cliframe.Global) int
RunCLI runs the command named name. The caller has checked Lookup.
type Result ¶
Result is what a command returns: a struct whose JSON form is the structured result, and the text for people.
type StaticCommand ¶
type StaticCommand struct {
Name string
Description string
Usage string // the synopsis, "mash serve [options]"
Examples []string
Arguments [][2]string
Options [][2]string
}
StaticCommand is a command that is not on the table (its parameters are not a struct) but whose --help is the same layout: the texts are the specification's. Arguments and Options are (label, description) rows, the label as printed ("address", "--wake-count <n>").
type Tool ¶
type Tool struct {
Name string
Description string
Schema map[string]any
CallStructured func(ctx context.Context, args json.RawMessage) (text string, structured any, err error)
}
Tool is one command as an MCP tool. CallStructured returns the text for the content block and the structured value (nil when the result is not a JSON object). An error's text is "error: MSG" plus "\nhint: HINT" when there is a hint.
internal/mcp.Tool has no structured result yet; MCP converts a Tool to one that returns the text only.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cmds holds the commands defined on cmdcore: each is a params struct, a result struct and a Run, served through both the command line and the MCP tool.
|
Package cmds holds the commands defined on cmdcore: each is a params struct, a result struct and a Run, served through both the command line and the MCP tool. |
|
Package render holds the small text helpers shared by the commands' human output: relative times, aligned tables, the terminal width, colour and plural counts.
|
Package render holds the small text helpers shared by the commands' human output: relative times, aligned tables, the terminal width, colour and plural counts. |