Documentation ¶
Index ¶
- func AddArgumentsToCobraCommand(cmd *cobra.Command, arguments []*ParameterDefinition) error
- func AddCommandsToRootCommand(rootCmd *cobra.Command, commands []CobraCommand, aliases []*CommandAlias) error
- func AddFlagsToCobraCommand(flagSet *flag.FlagSet, flags []*ParameterDefinition) error
- func GatherArguments(args []string, arguments []*ParameterDefinition, onlyProvided bool) (map[string]interface{}, error)
- func GatherFlagsFromCobraCommand(cmd *cobra.Command, params []*ParameterDefinition, onlyProvided bool) (map[string]interface{}, error)
- func GatherParametersFromCobraCommand(cmd *cobra.Command, description *CommandDescription, args []string) (map[string]interface{}, error)
- func InitializeStructFromParameterDefinitions(s interface{}, parameterDefinitions map[string]*ParameterDefinition) error
- func InitializeStructFromParameters(s interface{}, parameters map[string]interface{}) error
- func IsFileLoadingParameter(p ParameterType, v string) bool
- func IsListParameter(p ParameterType) bool
- 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{}, gp *GlazeProcessor) error
- func (a *CommandAlias) RunFromCobra(cmd *cobra.Command, args []string) error
- func (a *CommandAlias) String() string
- type CommandDescription
- type FSCommandLoader
- type GlazeProcessor
- type ParameterDefinition
- func (p *ParameterDefinition) CheckParameterDefaultValueValidity() error
- func (p *ParameterDefinition) Copy() *ParameterDefinition
- func (p *ParameterDefinition) ParseParameter(v []string) (interface{}, error)
- func (p *ParameterDefinition) SetValueFromDefault(value reflect.Value) error
- func (p *ParameterDefinition) String() string
- type ParameterType
- type YAMLCommandLoader
- type YAMLFSCommandLoader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddArgumentsToCobraCommand ¶ added in v0.2.6
func AddArgumentsToCobraCommand(cmd *cobra.Command, arguments []*ParameterDefinition) error
AddArgumentsToCobraCommand 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 AddFlagsToCobraCommand ¶ added in v0.2.6
func AddFlagsToCobraCommand(flagSet *flag.FlagSet, flags []*ParameterDefinition) error
AddFlagsToCobraCommand takes the parameters from a CommandDescription and converts them to cobra flags, before adding them to the Flags() of a the passed cobra command.
TODO(manuel, 2023-02-12) We need to handle arbitrary defaults here ¶
See https://github.com/go-go-golems/glazed/issues/132
Currently, usage of this functions just passes the defaults encoded in the metadata YAML files (for glazed flags at least), but really we want to override this on a per command basis easily without having to necessarily copy or mutate the parameters loaded from yaml.
One option would be to remove the defaults structs, and do the overloading by command with ParameterList manipulating functions, so that it is easy for the library user to override and further tweak the defaults.
Currently, that behaviour is encoded in the AddFieldsFilterFlags function itself.
What also needs to be considered is the bigger context that these declarative flags and arguments definitions are going to be used in a lot of different contexts, and might need to be overloaded and initialized in different ways.
For example: - REST API - CLI - GRPC service - TUI bubbletea UI - Web UI - declarative config files
--- 2023-02-12 - manuel
I went with the following solution:
One other option would be to pass this function a map with overloaded default, but while that feels easier and cleaner in the short term, I think it limits the concept of what it means for a library user to overload the defaults handling mechanism. This already becomes apparent in the FieldsFilterDefaults handling, where an empty list or a list containing "all" should be treated the same.
func GatherArguments ¶
func GatherArguments(args []string, arguments []*ParameterDefinition, 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 GatherFlagsFromCobraCommand ¶ added in v0.2.6
func GatherFlagsFromCobraCommand(cmd *cobra.Command, params []*ParameterDefinition, onlyProvided bool) (map[string]interface{}, error)
GatherFlagsFromCobraCommand 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 GatherParametersFromCobraCommand ¶ added in v0.2.6
func GatherParametersFromCobraCommand( cmd *cobra.Command, description *CommandDescription, args []string, ) (map[string]interface{}, error)
GatherParametersFromCobraCommand 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 InitializeStructFromParameterDefinitions ¶ added in v0.2.6
func InitializeStructFromParameterDefinitions(s interface{}, parameterDefinitions map[string]*ParameterDefinition) error
func InitializeStructFromParameters ¶ added in v0.2.6
func IsFileLoadingParameter ¶ added in v0.2.6
func IsFileLoadingParameter(p ParameterType, v string) bool
IsFileLoadingParameter returns true if the parameter type is one that loads a file, when provided with the given value. This slightly odd API is because some types like ParameterTypeKeyValue can be either a string or a file. A beginning character of @ indicates a file.
func IsListParameter ¶ added in v0.2.6
func IsListParameter(p ParameterType) bool
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(parameters map[string]interface{}, gp *GlazeProcessor) 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 LoadCommandAliasFromYAML ¶ added in v0.2.5
func LoadCommandAliasFromYAML(s io.Reader) ([]*CommandAlias, error)
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{}, gp *GlazeProcessor) error
func (*CommandAlias) RunFromCobra ¶
func (a *CommandAlias) RunFromCobra(cmd *cobra.Command, args []string) error
func (*CommandAlias) String ¶ added in v0.2.5
func (a *CommandAlias) String() string
type CommandDescription ¶
type CommandDescription struct { Name string `yaml:"name"` Short string `yaml:"short"` Long string `yaml:"long,omitempty"` Flags []*ParameterDefinition `yaml:"flags,omitempty"` Arguments []*ParameterDefinition `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 FSCommandLoader ¶ added in v0.2.5
type FSCommandLoader interface {
LoadCommandsFromFS(f fs.FS, dir string) ([]Command, []*CommandAlias, error)
}
FSCommandLoader is an interface that describes the most generic loader type, which is then used to load commands and command aliases from embedded queries and from "repository" directories used by glazed.
Examples of this pattern are used in sqleton, escuse-me and pinocchio.
type GlazeProcessor ¶ added in v0.2.6
type GlazeProcessor struct {
// contains filtered or unexported fields
}
func NewGlazeProcessor ¶ added in v0.2.6
func NewGlazeProcessor(of formatters.OutputFormatter, oms []middlewares.ObjectMiddleware) *GlazeProcessor
func (*GlazeProcessor) OutputFormatter ¶ added in v0.2.6
func (gp *GlazeProcessor) OutputFormatter() formatters.OutputFormatter
func (*GlazeProcessor) ProcessInputObject ¶ added in v0.2.6
func (gp *GlazeProcessor) ProcessInputObject(obj map[string]interface{}) error
TODO(2022-12-18, manuel) we should actually make it possible to order the columns https://github.com/go-go-golems/glazed/issues/56
type ParameterDefinition ¶ added in v0.2.6
type ParameterDefinition 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"` }
ParameterDefinition is a declarative way of describing a command line parameter. A ParameterDefinition 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 CloneParameterDefinitionsWithDefaultsStruct ¶ added in v0.2.6
func CloneParameterDefinitionsWithDefaultsStruct( parameterList []*ParameterDefinition, s interface{}, ) ([]*ParameterDefinition, error)
CloneParameterDefinitionsWithDefaultsStruct clones the parameter definitions and sets the default values from the struct's tag `glazed.parameter`.
TODO(manuel, 2023-02-12): This function is not necessary if we have a better way of initializing defaults
This is more of a placeholder while we are refactoring things for https://github.com/go-go-golems/glazed/issues/132
func InitFlagsFromYaml ¶ added in v0.2.6
func InitFlagsFromYaml(yamlContent []byte) (map[string]*ParameterDefinition, []*ParameterDefinition)
func (*ParameterDefinition) CheckParameterDefaultValueValidity ¶ added in v0.2.6
func (p *ParameterDefinition) CheckParameterDefaultValueValidity() error
func (*ParameterDefinition) Copy ¶ added in v0.2.6
func (p *ParameterDefinition) Copy() *ParameterDefinition
func (*ParameterDefinition) ParseParameter ¶ added in v0.2.6
func (p *ParameterDefinition) ParseParameter(v []string) (interface{}, error)
func (*ParameterDefinition) SetValueFromDefault ¶ added in v0.2.6
func (p *ParameterDefinition) SetValueFromDefault(value reflect.Value) error
SetValueFromDefault assigns the default value of the ParameterDefinition to the given value. If the Default value is nil, the value is set to the zero value of the type.
TODO(manuel, 2023-02-12) Not sure if the setting to the zero value of the type is the best idea, really.
func (*ParameterDefinition) String ¶ added in v0.2.6
func (p *ParameterDefinition) String() string
type ParameterType ¶
type ParameterType string
const ( ParameterTypeString ParameterType = "string" ParameterTypeStringFromFile ParameterType = "stringFromFile" ParameterTypeObjectListFromFile ParameterType = "objectListFromFile" ParameterTypeObjectFromFile ParameterType = "objectFromFile" // ParameterTypeKeyValue signals either a string with comma separate key-value options, or when beginning with @, a file with key-value options ParameterTypeKeyValue ParameterType = "keyValue" ParameterTypeInteger ParameterType = "int" ParameterTypeFloat ParameterType = "float" ParameterTypeBool ParameterType = "bool" ParameterTypeDate ParameterType = "date" ParameterTypeStringList ParameterType = "stringList" ParameterTypeIntegerList ParameterType = "intList" ParameterTypeFloatList ParameterType = "floatList" ParameterTypeChoice ParameterType = "choice" )
type YAMLCommandLoader ¶ added in v0.2.5
type YAMLCommandLoader interface { LoadCommandFromYAML(s io.Reader) ([]Command, error) LoadCommandAliasFromYAML(s io.Reader) ([]*CommandAlias, error) }
YAMLCommandLoader is an interface that allows an application using the glazed library to loader commands from YAML files.
type YAMLFSCommandLoader ¶ added in v0.2.5
type YAMLFSCommandLoader struct {
// contains filtered or unexported fields
}
func NewYAMLFSCommandLoader ¶ added in v0.2.5
func NewYAMLFSCommandLoader( loader YAMLCommandLoader, sourceName string, cmdRoot string, ) *YAMLFSCommandLoader
func (*YAMLFSCommandLoader) LoadCommandsFromFS ¶ added in v0.2.5
func (l *YAMLFSCommandLoader) LoadCommandsFromFS(f fs.FS, dir string) ([]Command, []*CommandAlias, error)