cmdtree

package
v2.58.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 5 Imported by: 1 module, 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 cmdtree builds the command tree of an extension from multiple command sources (e.g. API specifications and scripts). Commands are merged by their path, so different sources can contribute commands to the same command group.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsHelpRequest

func IsHelpRequest(args []string) bool

IsHelpRequest returns true if the arguments only request help for the command

Types

type Conflict

type Conflict struct {
	// Path of the conflicting command (space separated)
	Path string

	// Kept is the origin of the command (or group) which is used
	Kept string

	// Dropped is the origin of the command which is ignored
	Dropped string
}

Conflict describes a command which was dropped because another command was registered at the same path

func (Conflict) String

func (c Conflict) String() string

type Leaf

type Leaf struct {
	// Source of the command, used to resolve conflicts
	Source SourceKind

	// Origin of the command, e.g. a file path. Only used for conflict reporting
	Origin string

	// Build creates the cobra command. The returned command's Use field is set to the
	// last element of the command's path
	Build func() (*cobra.Command, error)
}

Leaf is a runnable command

type Mount

type Mount struct {
	// Origin of the mount, e.g. a file path. Only used for conflict reporting
	Origin string

	// Short description, used if the mount path is not also a command group with a description
	Short string

	// Long description which is appended to the group's long description
	Long string

	// Configure is called with the cobra command which is created for the mount path. It
	// should set RunE (and optionally ValidArgsFunction). It is called once for the mount
	Configure func(cmd *cobra.Command) error
}

Mount passes all arguments under a command path to a single handler (e.g. the extension's binary). A mount is not a leaf: other commands can be added below the mount path, and they take precedence over the mount. Any other arguments are passed to the mount.

type SourceKind

type SourceKind int

SourceKind is the kind of source which provides a command. Lower values have a higher precedence when two sources define the same command.

const (
	// SourceAPI is a command defined in an API specification file (api/*.yaml)
	SourceAPI SourceKind = iota
	// SourceBinary is a command declared in the extension manifest (extension.yaml) which runs the extension's binary
	SourceBinary
	// SourceScript is an executable file under the commands/ folder
	SourceScript
)

func (SourceKind) String

func (s SourceKind) String() string

type Tree

type Tree struct {

	// OnConflict is called for every command which is dropped due to a conflict
	OnConflict func(Conflict)
	// contains filtered or unexported fields
}

Tree is the command tree of a single extension

func New

func New(name string, short string) *Tree

New creates a new command tree. name is the root command name (i.e. the extension name)

func (*Tree) AddGroup

func (t *Tree) AddGroup(path []string, short, long, origin string)

AddGroup declares a command group at the given path. Groups are merged with any existing group at the same path. The first non-empty description is used, and it replaces the generated default description.

func (*Tree) AddLeaf

func (t *Tree) AddLeaf(path []string, leaf Leaf)

AddLeaf adds a runnable command at the given path. Missing parent groups are created.

Conflicts are resolved as follows:

  • if a group already exists at the path, the leaf is dropped
  • if a leaf already exists at the path, the leaf with the higher precedence (lower SourceKind) is kept. For equal precedence, the first leaf is kept.

func (*Tree) AddMount

func (t *Tree) AddMount(path []string, mount Mount)

AddMount adds a mount at the given path. An empty path mounts at the root of the tree. If a runnable command already exists at the path, the mount is dropped. If a group exists at the path, then the group's commands are used for their paths, and any other arguments are passed to the mount. If more than one mount is added at the same path, the first one is kept.

func (*Tree) IsEmpty

func (t *Tree) IsEmpty() bool

IsEmpty returns true if the tree does not contain any runnable commands

func (*Tree) ToCobra

func (t *Tree) ToCobra() (*cobra.Command, error)

ToCobra converts the tree into cobra commands. Groups which do not contain any runnable commands are omitted. Nil is returned if the tree has no runnable commands. Errors from building individual commands are collected and returned together with the remaining commands.

Jump to

Keyboard shortcuts

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