cmds

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 17 Imported by: 66

Documentation

Index

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

XXX(manuel, 2023-01-25) Figure out how to refactor parents/aliases

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 LoadCommandsFromDirectory

func LoadCommandsFromDirectory(loader CommandLoader, dir string, cmdRoot string) ([]Command, []*CommandAlias, error)

func LoadCommandsFromEmbedFS

func LoadCommandsFromEmbedFS(loader CommandLoader, f embed.FS, 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)
}

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"`
}

func (*CommandAlias) BuildCobraCommand

func (a *CommandAlias) BuildCobraCommand() (*cobra.Command, error)

func (*CommandAlias) Description

func (a *CommandAlias) Description() *CommandDescription

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  string   `yaml:",omitempty"`
}

type CommandLoader

type CommandLoader interface {
	LoadCommandFromYAML(s io.Reader) ([]Command, error)
	LoadCommandAliasFromYAML(s io.Reader) ([]*CommandAlias, error)
}

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"`
}

func (*Parameter) CheckParameterDefaultValueValidity

func (p *Parameter) CheckParameterDefaultValueValidity() error

func (*Parameter) Copy

func (p *Parameter) Copy() *Parameter

func (*Parameter) ParseParameter

func (p *Parameter) ParseParameter(v []string) (interface{}, error)

type ParameterType

type ParameterType string
const (
	ParameterTypeString         ParameterType = "string"
	ParameterTypeStringFromFile ParameterType = "stringFromFile"
	// 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"
)

Jump to

Keyboard shortcuts

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