Documentation
¶
Overview ¶
Package clihelp provides the shared cobra infrastructure used by cmd/lym and every CLI module. It holds the per-invocation exit state. Its Execute and ExecuteIn seams run a module's command tree in process over an io.Writer. It also holds the RunE wrapper for legacy handlers and the helpers that wrap Cobra errors in the JSON envelope.
It owns the global --json flag (InstallJSONHelp). Under it, every command, leaf or group, prints its help as structured JSON and exits 0, and no command runs. A command that declares its own local --json flag shadows the global one and keeps its own meaning.
It owns the readable summary of operator verbs (UseReadableOutput). Operator-audience commands print each JSON envelope line as a short summary when their output is a terminal; the global --envelope flag keeps the envelope there. An output that is no terminal, such as an agent's or a script's, always gets the envelope.
The command index appends a command's index note (IndexNoteAnnotation). It prints the note only in the audience that IndexNoteAudienceAnnotation names; a note without that annotation never prints.
Index ¶
- Constants
- Variables
- func Abort(ctx context.Context, code int)
- func Execute(cmd *cobra.Command, out io.Writer, args []string) int
- func ExecuteIn(cmd *cobra.Command, cwd string, out io.Writer, args []string) int
- func GroupRunE(cmd *cobra.Command, args []string) error
- func InstallJSONHelp(root *cobra.Command)
- func InstallReadableOutput(root *cobra.Command)
- func Invocable(cmd *cobra.Command) bool
- func NewExitContext(parent context.Context) (context.Context, *exitState)
- func RenderIndex(root *cobra.Command, audience string) string
- func RunRoot(cmd *cobra.Command, out io.Writer) int
- func RunRootCtx(ctx context.Context, cmd *cobra.Command, out io.Writer) int
- func SetExit(ctx context.Context, code int)
- func ShouldAbort(ctx context.Context) bool
- func UseReadableOutput(cmd *cobra.Command, isTerminal func(io.Writer) bool)
- func WrapRun(fn func(out io.Writer, args []string) int) func(*cobra.Command, []string) error
- func WrapRunCtx(fn func(ctx context.Context, out io.Writer, args []string) int) func(*cobra.Command, []string) error
- func WriterIsTerminal(w io.Writer) bool
Constants ¶
const AnnotationEnabled = "true"
AnnotationEnabled is the one value that reads as "on" for every annotation key in this package.
const AudienceAnnotation = "lym.audience"
AudienceAnnotation is the cobra-annotation key an invocable command carries to name who calls it. Its value is one of the audience values, and the command index lists a command only under the audience it names.
const AudienceInternal = "internal"
AudienceInternal marks a command lym itself runs, such as a daemon or a pinned key binding.
const AudienceOperator = "operator"
AudienceOperator marks a command the operator, or the orch acting for them, runs by hand.
const AudienceRole = "role"
AudienceRole marks a command a spawned role agent runs, named by that role's prompt.
const EnvelopeFlagUsage = "print the full JSON envelope where an operator verb on a terminal prints its readable summary"
EnvelopeFlagUsage is the help text of the global --envelope flag.
const IndexNoteAnnotation = "lym.index-note"
IndexNoteAnnotation is the cobra-annotation key whose value the command index appends to the command's line. The value is appended after one space. The note prints only when the index renders the audience named by the `lym.index-note-audience` annotation; a note without that annotation never prints.
const IndexNoteAudienceAnnotation = "lym.index-note-audience"
IndexNoteAudienceAnnotation is the cobra-annotation key naming the audience whose index prints the command's note. The command index appends a command's index note only when rendering that audience. It prints no note for a command without this annotation.
const JSONFlagUsage = "print the command's help as structured JSON and exit 0; the command itself never runs"
JSONFlagUsage is the help text of the global --json flag.
const RunProcessAnnotation = "lym.run-process"
RunProcessAnnotation is the cobra-annotation key a command sets to mark its process as a run's own. A pause holds the network calls of such a process and of every lym process it starts; those of any other command pass a pause. Only the value "true" marks a command.
const SkipStencilSeedAnnotation = "lym.skip-stencil-seed"
SkipStencilSeedAnnotation is the cobra-annotation key a command sets to decline the root pre-run's stencil-seed pass. Declining is all-or-nothing per command: it is for a command that reads no stencils and is expected to stay silent. A value other than "true" never opts out, so a "false" cannot silently read as an opt-out.
Variables ¶
var Audiences = []string{AudienceOperator, AudienceRole, AudienceInternal}
Audiences is the closed set of audience values, in index order.
Functions ¶
func Abort ¶
Abort records code as the exit code of the invocation carried by ctx and sets the abort flag. Subsequent RunE bodies then short-circuit without executing. A PersistentPreRunE calls it on a fatal setup error. That keeps leaf commands from running against an uninitialised environment. It is a no-op when code is zero or when ctx carries no exit state.
func Execute ¶
Execute runs a module's command tree in process, as tests drive a module without the lym root. It writes stdout and stderr both into out, so in-process tests capture all output from one buffer. It sets args and delegates to RunRoot for the context, the silence flags, and the JSON error wrapping.
func ExecuteIn ¶
ExecuteIn runs cmd with cwd seeded into its execution context. Command bodies that call lymcwd.CwdFrom(cmd.Context()) see cwd instead of the process working directory. It sets cmd's output and arguments first, then delegates to RunRootCtx. cwd must be an absolute, non-empty path: lymcwd.WithCwd panics on an empty or relative cwd.
func GroupRunE ¶
GroupRunE is the RunE for a parent command whose children are subcommands. With arguments it returns an error naming the unknown subcommand. Without arguments it prints the command's help. Wire it as cmd.RunE = clihelp.GroupRunE on each group command: a bare invocation then prints help, and an unknown subcommand emits a JSON error envelope through RunRoot.
func InstallJSONHelp ¶
InstallJSONHelp declares the global --json flag and a shared --help/-h flag as persistent flags on root. It also installs a HelpFunc. It renders JSON help when --json is set and delegates to cobra's default renderer otherwise. Every descendant inherits both flags and the HelpFunc, so commands added after this call are covered: under --json, every command, leaf or group, prints its JSON help and exits 0 without running. A descendant that declares its own local json flag shadows the global one, which then stays unset for that command, and the command runs with its own --json meaning. Call InstallJSONHelp once during root command construction.
func InstallReadableOutput ¶
InstallReadableOutput declares the global --envelope flag as a persistent flag on root. A command that declares its own envelope flag shadows the global one. When that flag is set, the command still prints the envelope. Call InstallReadableOutput once during root command construction.
func Invocable ¶
Invocable reports whether cmd takes an audience of its own. A leaf always does. A group does when it is runnable and either its Use carries a token after its name, or it has a visible flag of its own. A group's persistent flags never qualify it, since they configure its children.
func NewExitContext ¶
NewExitContext allocates a fresh *exitState, stores it in a child context derived from parent, and returns both. Call it once per CLI invocation. Never store the returned *exitState in a package-level variable, since that would break parallel-test isolation.
func RenderIndex ¶
RenderIndex renders the nested markdown list of every command under root whose audience annotation equals audience. It indents two spaces per level, and lines omit `lym` and the parent path, which the nesting implies. A command that does not print in leaf form prints in group form when a descendant prints. An invocable command prints in leaf form when its audience annotation matches. A command without an annotation never prints in leaf form. A command's index note prints only in the audience its IndexNoteAudienceAnnotation names. It walks root in memory and reads no file.
func RunRoot ¶
RunRoot runs cmd under a fresh exit state with a background context, and returns the exit code. It silences cobra's error and usage output. On a cobra error it writes the JSON error envelope to out and returns 1. Callers configure cmd's output writers and args first. cmd/lym and Execute both go through RunRoot, so the error wrapping has one implementation.
func RunRootCtx ¶
RunRootCtx runs cmd under a fresh exit state derived from ctx, and returns the exit code. It silences cobra's error and usage output. On a cobra error it writes the JSON error envelope to out and returns 1. Otherwise it returns the exit code recorded by SetExit. Callers configure cmd's output writers and args before the call; this function supplies only the context, the silence flags, and the error wrapping.
func SetExit ¶
SetExit records code as the exit code of the invocation carried by ctx. A zero code is ignored, so a success never overwrites a failure code. It is also a no-op when ctx carries no exit state.
func ShouldAbort ¶
ShouldAbort reports whether Abort was called on the exit state carried by ctx. A RunE body should return nil at its start when this reports true. A PersistentPreRunE that aborted has already written the error response and recorded the exit code.
func UseReadableOutput ¶
UseReadableOutput points cmd's output at an output.SummaryWriter when cmd prints the readable summary. It flushes that writer when the invocation ends. cmd prints the summary only when it carries the operator audience and its output is a terminal per isTerminal. It also requires that no envelope flag is set on cmd. Likewise a command that declares its own json flag is skipped, since it renders its own view for a terminal. A person at a terminal reads each envelope line as its summary. An agent or script, whose output is not a terminal, reads the envelope.
func WrapRun ¶
WrapRun adapts a legacy handler function with signature func(io.Writer, []string) int into a cobra RunE. The returned RunE skips fn and returns nil when Abort was called on the command's context, so a failing PersistentPreRunE keeps every leaf command body from running. Otherwise it passes fn's exit code to SetExit. It always returns nil, so cobra does not double-print over JSON output the handler already wrote.
func WrapRunCtx ¶
func WrapRunCtx(fn func(ctx context.Context, out io.Writer, args []string) int) func(*cobra.Command, []string) error
WrapRunCtx adapts a handler with signature func(context.Context, io.Writer, []string) int into a cobra RunE. The handler receives cmd.Context() as its first argument, so it can read an injected cwd through lymcwd.CwdFrom. The returned RunE skips fn and returns nil when Abort was called on the command's context. Otherwise it passes fn's exit code to SetExit. It always returns nil, so cobra does not double-print over JSON output the handler already wrote.
func WriterIsTerminal ¶
WriterIsTerminal reports whether w is an *os.File whose descriptor is a terminal.
Types ¶
This section is empty.