Documentation
¶
Index ¶
- func AddArguments(cmd *cobra.Command, description *CommandDescription) error
- func AddCommandsToRootCommand(rootCmd *cobra.Command, commands []CobraCommand, aliases []*CommandAlias) error
- func AddFlags(cmd *cobra.Command, description *CommandDescription) error
- func GatherArguments(args []string, arguments []*Parameter, onlyProvided bool) (map[string]interface{}, error)
- func GatherFlags(cmd *cobra.Command, params []*Parameter, onlyProvided bool) (map[string]interface{}, error)
- func GatherParameters(cmd *cobra.Command, description *CommandDescription, args []string) (map[string]interface{}, error)
- func LoadCommandsFromFS(loader CommandLoader, f fs.FS, sourceName string, dir string, cmdRoot string) ([]Command, []*CommandAlias, error)
- func NewCobraCommand(s CobraCommand) (*cobra.Command, error)
- type CobraCommand
- type Command
- type CommandAlias
- func (a *CommandAlias) BuildCobraCommand() (*cobra.Command, error)
- func (a *CommandAlias) Description() *CommandDescription
- func (a *CommandAlias) IsValid() bool
- func (a *CommandAlias) Run(parameters map[string]interface{}) error
- func (a *CommandAlias) RunFromCobra(cmd *cobra.Command, args []string) error
- type CommandDescription
- type CommandLoader
- type Parameter
- type ParameterType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddArguments ¶
func AddArguments(cmd *cobra.Command, description *CommandDescription) error
AddArguments adds the arguments (not the flags) of a CommandDescription to a cobra command as positional arguments. An optional argument cannot be followed by a required argument. Similarly, a list of arguments cannot be followed by any argument (since we otherwise wouldn't know how many belong to the list and where to do the cut off).
func AddCommandsToRootCommand ¶
func AddCommandsToRootCommand(rootCmd *cobra.Command, commands []CobraCommand, aliases []*CommandAlias) error
func AddFlags ¶
func AddFlags(cmd *cobra.Command, description *CommandDescription) error
AddFlags takes the parameters from a CommandDescription and converts them to cobra flags, before adding them to the Flags() of a the passed cobra command.
func GatherArguments ¶
func GatherArguments(args []string, arguments []*Parameter, onlyProvided bool) (map[string]interface{}, error)
GatherArguments parses the positional arguments passed as a list of strings into a map of parsed values. If onlyProvided is true, then only arguments that are provided are returned (i.e. the default values are not included).
func GatherFlags ¶
func GatherFlags(cmd *cobra.Command, params []*Parameter, onlyProvided bool) (map[string]interface{}, error)
GatherFlags gathers the flags from the cobra command, and parses them according to the parameter description passed in params. The result is a map of parameter names to parsed values. If onlyProvided is true, only parameters that are provided by the user are returned (i.e. not the default values). If a parameter cannot be parsed correctly, or is missing even though it is not optional, an error is returned.
func GatherParameters ¶
func GatherParameters( cmd *cobra.Command, description *CommandDescription, args []string, ) (map[string]interface{}, error)
GatherParameters takes a cobra command, an argument list as well as a description of the sqleton command arguments, and returns a list of parsed parameters as a hashmap. It does so by parsing both the flags and the positional arguments.
func LoadCommandsFromFS ¶ added in v0.2.4
func LoadCommandsFromFS(loader CommandLoader, f fs.FS, sourceName string, dir string, cmdRoot string) ([]Command, []*CommandAlias, error)
func NewCobraCommand ¶
func NewCobraCommand(s CobraCommand) (*cobra.Command, error)
Types ¶
type CobraCommand ¶
type CobraCommand interface {
Command
RunFromCobra(cmd *cobra.Command, args []string) error
BuildCobraCommand() (*cobra.Command, error)
}
CobraCommand is a subset of Command than can be registered as a cobra.Command by using BuildCobraCommand, and can then be executed when cobra runs it through RunFromCobra, passing in the full cobra object in case the user wants to overload anything.
type Command ¶
type Command interface {
Run(map[string]interface{}) error
Description() *CommandDescription
}
type CommandAlias ¶
type CommandAlias struct {
Name string `yaml:"name"`
AliasFor string `yaml:"aliasFor"`
Flags map[string]string `yaml:"flags,omitempty"`
Arguments []string `yaml:"arguments,omitempty"`
AliasedCommand Command `yaml:",omitempty"`
Parents []string `yaml:",omitempty"`
Source string `yaml:",omitempty"`
}
CommandAlias defines a struct that should be able to define generic aliases for any kind of command line applications, by providing overrides for certain flags (prepopulating them with certain flags and arguments, basically)
func (*CommandAlias) BuildCobraCommand ¶
func (a *CommandAlias) BuildCobraCommand() (*cobra.Command, error)
func (*CommandAlias) Description ¶
func (a *CommandAlias) Description() *CommandDescription
Description returns the CommandDescription of an alias. It computes it at runtime by loading the aliased command's Description() and making copies of its flags and arguments. This is necessary because they get mutated at runtime with various defaults, depending on where they come from.
func (*CommandAlias) IsValid ¶
func (a *CommandAlias) IsValid() bool
func (*CommandAlias) Run ¶
func (a *CommandAlias) Run(parameters map[string]interface{}) error
func (*CommandAlias) RunFromCobra ¶
func (a *CommandAlias) RunFromCobra(cmd *cobra.Command, args []string) error
type CommandDescription ¶
type CommandDescription struct {
Name string `yaml:"name"`
Short string `yaml:"short"`
Long string `yaml:"long,omitempty"`
Flags []*Parameter `yaml:"flags,omitempty"`
Arguments []*Parameter `yaml:"arguments,omitempty"`
Parents []string `yaml:",omitempty"`
// Source indicates where the command was loaded from, to make debugging easier.
Source string `yaml:",omitempty"`
}
CommandDescription contains the necessary information for registering a command with cobra. Because a command gets registered in a verb tree, a full list of Parents all the way to the root needs to be provided.
type CommandLoader ¶
type CommandLoader interface {
LoadCommandFromYAML(s io.Reader) ([]Command, error)
LoadCommandAliasFromYAML(s io.Reader) ([]*CommandAlias, error)
}
CommandLoader is an interface that allows an application using the glazed library to load commands from YAML files.
TODO(2023-02-07, manuel) Refactor this to use an FS instead In fact, this might not even be fully necessary, let the application walk a FS and do the loading.
type Parameter ¶
type Parameter struct {
Name string `yaml:"name"`
ShortFlag string `yaml:"shortFlag,omitempty"`
Type ParameterType `yaml:"type"`
Help string `yaml:"help,omitempty"`
Default interface{} `yaml:"default,omitempty"`
Choices []string `yaml:"choices,omitempty"`
Required bool `yaml:"required,omitempty"`
}
Parameter is a declarative way of describing a command line parameter. A Parameter can be either a Flag or an Argument. Along with metadata (Name, Help) that is useful for help, it also specifies a Type, a Default value and if it is Required.
func (*Parameter) CheckParameterDefaultValueValidity ¶
func (*Parameter) ParseParameter ¶
type ParameterType ¶
type ParameterType string
const ( ParameterTypeString ParameterType = "string" ParameterTypeStringFromFile ParameterType = "stringFromFile" // ParameterTypeObjectFromFile - load structure from json/yaml/csv file ParameterTypeObjectFromFile ParameterType = "objectFromFile" ParameterTypeInteger ParameterType = "int" ParameterTypeFloat ParameterType = "float" ParameterTypeBool ParameterType = "bool" ParameterTypeDate ParameterType = "date" ParameterTypeStringList ParameterType = "stringList" ParameterTypeIntegerList ParameterType = "intList" ParameterTypeFloatList ParameterType = "floatList" ParameterTypeChoice ParameterType = "choice" )