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 ¶
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
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 (*Tree) AddGroup ¶
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 ¶
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 ¶
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.