Help us shape the future of Go! Take the 2026 Go Developer Survey

cmdregistry

package
v1.4.13 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cmdregistry derives the deterministic command registry document (contract:cli.command-registry v1) from a cobra tree, the declared canon data and the JSON data-type map.

Purpose: one machine-readable description of the CLI command surface. Inputs: a cobra root (read only), *canon.File, a map of registered JSON

data types keyed by command path without "nself ", BuildOptions.

Outputs: *Registry, marshalled with Marshal (2-space indent, no HTML

escaping, trailing newline). Field names and order are a
cross-repo contract; a rename after merge is a v2.

Constraints: stdlib + cobra + pflag + internal/canon only. Build never

mutates the tree and does not import compat (the caller passes
compat.V15() as BuildOptions.V15).

Index

Constants

View Source
const RootBindingPath = "."

RootBindingPath is the FlagEnv key of the root command's own flags.

View Source
const SchemaVersion = "1"

SchemaVersion is the registry document version.

Variables

This section is empty.

Functions

func ApplyFlagEnv

func ApplyFlagEnv(reg *Registry, flagEnv map[string]map[string]string, moves []PathMove, v15 bool) error

ApplyFlagEnv sets Flag.env on every flag named in flagEnv. flagEnv is keyed by canonical command path (RootBindingPath for the root) then flag name. In v1.4 mode (v15 false) the paths are translated through moves first. All violations are returned in one error.

func EffectiveOutput

func EffectiveOutput(cmd *Command, set map[string]bool) string

EffectiveOutput selects stream when a set flag declares a stream override.

func EffectiveSideEffect

func EffectiveSideEffect(cmd *Command, set map[string]bool) string

EffectiveSideEffect is the highest class produced by set flag overrides.

func RoutePath

func RoutePath(path string) string

RoutePath converts a command path to its HTTP route.

func ToolName

func ToolName(path string) string

ToolName converts a command path to its MCP tool identifier.

func V14Path

func V14Path(canonical string, moves []PathMove) string

V14Path maps a canonical command path to its v1.4 spelling.

func ValidateToolNames

func ValidateToolNames(reg *Registry) error

ValidateToolNames rejects invalid or colliding generated tool identifiers.

Types

type Arg

type Arg struct {
	Name     string `json:"name"`
	Required bool   `json:"required"`
	Variadic bool   `json:"variadic"`
	Secret   bool   `json:"secret"`
}

Arg is one positional argument parsed from the command's Use string.

type BuildOptions

type BuildOptions struct {
	// V15 selects ExitCodesV15 and enables V15OnlyEnvelope paths. The caller
	// passes compat.V15().
	V15 bool
	// V15OnlyEnvelope lists paths (without "nself ") whose registered data
	// type reports json envelope only when V15 is true (EPIC D8).
	V15OnlyEnvelope map[string]bool
}

BuildOptions selects the compat mode and the v1.5-only envelope paths.

type Command

type Command struct {
	Path       string            `json:"path"`
	Name       string            `json:"name"`
	Parent     string            `json:"parent"`
	Summary    string            `json:"summary"`
	Hidden     bool              `json:"hidden"`
	Deprecated *string           `json:"deprecated"`
	Aliases    []string          `json:"aliases"`
	Group      *string           `json:"group"`
	Runnable   bool              `json:"runnable"`
	Args       []Arg             `json:"args"`
	Flags      []Flag            `json:"flags"`
	Canon      string            `json:"canon"`
	Plugin     *string           `json:"plugin,omitempty"`
	Target     *string           `json:"target"`
	SideEffect string            `json:"side_effect"`
	Output     string            `json:"output"`
	JSON       string            `json:"json"`
	DataSchema *string           `json:"data_schema"`
	ExitCodes  map[string]string `json:"exit_codes"`
	Confirm    *Confirm          `json:"confirm"`
	Surface    string            `json:"surface"`
}

Command is one non-root cobra command.

type Confirm

type Confirm struct {
	Flags []string  `json:"flags"`
	Plan  *PlanForm `json:"plan"`
}

Confirm is the registry's machine-readable confirmation declaration.

type Counts

type Counts struct {
	Commands        int      `json:"commands"`
	TopLevel        int      `json:"top_level"`
	Core            int      `json:"core"`
	Pending         int      `json:"pending"`
	DeprecatedShims int      `json:"deprecated_shims"`
	Plugin          int      `json:"plugin,omitempty"`
	CoreMissing     []string `json:"core_missing"`
	JSONEnvelope    int      `json:"json_envelope"`
	JSONLegacy      int      `json:"json_legacy"`
}

Counts are the derived totals.

type Flag

type Flag struct {
	Name       string  `json:"name"`
	Shorthand  *string `json:"shorthand"`
	Type       string  `json:"type"`
	Default    string  `json:"default"`
	Usage      string  `json:"usage"`
	Hidden     bool    `json:"hidden"`
	Deprecated *string `json:"deprecated"`
	Required   bool    `json:"required"`
	Persistent bool    `json:"persistent"`
	Env        *string `json:"env"`
	SideEffect *string `json:"side_effect"`
	JSON       *string `json:"json"`
	Output     *string `json:"output"`
	CLIOnly    bool    `json:"cli_only"`
	Secret     bool    `json:"secret"`
}

Flag is one flag declared on a command (or on the root).

type PathMove

type PathMove struct{ From, To string }

PathMove is one canon move row: the v1.4 spelling From became the canonical path To (both without the leading "nself ", words joined by one space).

type PlanForm

type PlanForm struct {
	Flag   string `json:"flag"`
	IDFlag string `json:"id_flag"`
}

PlanForm names the boolean plan flag and string plan identifier flag.

type Registry

type Registry struct {
	SchemaVersion string    `json:"schema_version"`
	Verbs         []string  `json:"verbs"`
	Root          Root      `json:"root"`
	Commands      []Command `json:"commands"`
	Counts        Counts    `json:"counts"`
	// contains filtered or unexported fields
}

Registry is the whole document.

func Build

func Build(root *cobra.Command, c *canon.File, dataTypes map[string]any, opts BuildOptions) (*Registry, error)

Build derives the registry from root, the declared canon data c and the registered JSON data types (keys are command paths without the leading "nself "; values are only tested for presence). It never mutates the cobra tree. Every violated rule is collected into one *canon.ValidationError that names each offending path; on error the registry is nil.

func (*Registry) Lookup

func (r *Registry) Lookup(path string) (*Command, bool)

Lookup finds a command by its full path ("nself config get"); the path without the root name ("config get") is accepted too.

func (*Registry) Marshal

func (r *Registry) Marshal() ([]byte, error)

Marshal renders the registry as the published JSON document: 2-space indent, no HTML escaping, trailing newline.

func (*Registry) Subtree

func (r *Registry) Subtree(path string) *Registry

Subtree returns a registry holding the command at path and every descendant (nil when the path is unknown). Counts are recomputed over the subset except core_missing, which stays registry-wide. The result shares no slice with r.

type Root

type Root struct {
	Summary string `json:"summary"`
	Flags   []Flag `json:"flags"`
}

Root describes the root command.

Jump to

Keyboard shortcuts

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