commands

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolFromString

func BoolFromString(b string) (*upcloud.Boolean, error)

BoolFromString parses a string and returns *upcloud.Boolean

func CompletionEscape

func CompletionEscape(s string) string

CompletionEscape escapes a string according to completion rules (?) in effect, this means that the string will be quoted with double quotes if it contains a space or parentheses.

func CustomBashCompletionFunc

func CustomBashCompletionFunc(name string) string

CustomBashCompletionFunc returns a bash completion function used by cobras bash completion generator

func MatchStringPrefix

func MatchStringPrefix(vals []string, key string, caseSensitive bool) []string

MatchStringPrefix returns a list of string in vals which have a prefix as specified in key. Quotes are removed from key and output strings are escaped according to completion rules

func Parse

func Parse(in string) ([]string, error)

Parse parses a complex, querystring-type argument from in and returns all the parts found eg. `--foo bar=baz,flop=flip` returns `[]string{"bar","baz","flop","flip"}`

func SearchResources

func SearchResources(
	ids []string,
	searchFn func(id string) (interface{}, error),
	getUUID func(interface{}) string,
) ([]string, error)

SearchResources is a convenience method to map a list of resources to uuids. Any input strings that are uuids are returned as such and any other string is passed on to searchFn, the results of which are passed on to getUUID which is expected to return a uuid.

func StateColour

func StateColour(state string) text.Colors

StateColour is a helper mapping states to colors

func ToArray

func ToArray(in interface{}) []interface{}

ToArray turns an interface{} to a slice of interface{}s. If the underlying type is also a slice, the elements will be returned as the return values elements.. Otherwise, the input element is wrapped in a slice.

Types

type BaseCommand

type BaseCommand struct {
	// contains filtered or unexported fields
}

BaseCommand is the base type for all commands, implementing Command

func New

func New(name, usage string) *BaseCommand

New returns a BaseCommand that implements Command. It is used as a base to create custom commands from.

func (*BaseCommand) AddFlags

func (s *BaseCommand) AddFlags(flags *pflag.FlagSet)

AddFlags adds a flagset to the command and binds config value into it with namespace

func (*BaseCommand) AddPersistentFlags

func (s *BaseCommand) AddPersistentFlags(flags *pflag.FlagSet)

AddPersistentFlags adds a persistent flagset to the command and binds config value into it with namespace

func (*BaseCommand) AddVisibleColumnsFlag

func (s *BaseCommand) AddVisibleColumnsFlag(flags *pflag.FlagSet, dstPtr *[]string, available, defaults []string)

AddVisibleColumnsFlag is a convenience method to set a common flag '--columns' to commands

func (*BaseCommand) ArgCompletion

func (s *BaseCommand) ArgCompletion(fn func(toComplete string) ([]string, cobra.ShellCompDirective))

ArgCompletion is a convenience method to set upctl-specific completion function to the underlying cobra.Command

func (*BaseCommand) Children

func (s *BaseCommand) Children() []Command

Children returns a list of all the child commands of this command (eg. including the children of children)

func (*BaseCommand) Cobra

func (s *BaseCommand) Cobra() *cobra.Command

Cobra returns the underlying *cobra.Command

func (*BaseCommand) Config

func (s *BaseCommand) Config() *config.Config

Config implements Command.Config, returning the *config.Config of the command

func (*BaseCommand) ConfigLoader

func (s *BaseCommand) ConfigLoader() func(config *config.Config) error

ConfigLoader implements Command.ConfigLoader, returning the specified ConfigLoader

func (*BaseCommand) DeleteChild

func (s *BaseCommand) DeleteChild(command Command)

DeleteChild removes command from the children of this command

func (*BaseCommand) HandleError

func (s *BaseCommand) HandleError(err error)

HandleError is used to handle errors from the main command

func (*BaseCommand) HandleOutput

func (s *BaseCommand) HandleOutput(io.Writer, interface{}) error

HandleOutput should be overwritten by the actual command implementations It is expected to write output, given in out, to writer

func (*BaseCommand) InitCommand

func (s *BaseCommand) InitCommand()

InitCommand can be overriden to handle flag registration. A hook to handle flag registration. The config values are not available during this hook. Register a cobra hook to use them. You can set defaults though.

func (*BaseCommand) MakeExecuteCommand

func (s *BaseCommand) MakeExecuteCommand() func(args []string) (interface{}, error)

MakeExecuteCommand should be overwritten by the actual command implementations The function returned is ran during the 'regular' execute phase and the returned value is returned to the user, formatted as requested.

func (*BaseCommand) MakePersistentPreExecuteCommand

func (s *BaseCommand) MakePersistentPreExecuteCommand() func(args []string) error

MakePersistentPreExecuteCommand should be overwritten by the actual command implementations The function returned is ran before PreExecuteCommand().

func (*BaseCommand) MakePreExecuteCommand

func (s *BaseCommand) MakePreExecuteCommand() func(args []string) error

MakePreExecuteCommand should be overwritten by the actual command implementations The function returned is ran before the 'regular' execute phase.

func (*BaseCommand) Name

func (s *BaseCommand) Name() string

Name returns the name of the command

func (*BaseCommand) Namespace

func (s *BaseCommand) Namespace() string

Namespace returns the namespace of this command from the chain of parent commands The format is cmdRoot.child1.child2.childN No namespace is returned for the root command (parent == nil)

func (*BaseCommand) Parent

func (s *BaseCommand) Parent() Command

Parent returns the parent of the command

func (*BaseCommand) SetChild

func (s *BaseCommand) SetChild(command Command)

SetChild sets command as the child of this command

func (*BaseCommand) SetConfig

func (s *BaseCommand) SetConfig(config *config.Config)

SetConfig sets the configuration used

func (*BaseCommand) SetConfigLoader

func (s *BaseCommand) SetConfigLoader(fn func(config *config.Config) error)

SetConfigLoader implements Command.SetConfigLoader, setting internal config loader

func (*BaseCommand) SetFlags

func (s *BaseCommand) SetFlags(flags []string) error

SetFlags parses the given flags

func (*BaseCommand) SetParent

func (s *BaseCommand) SetParent(command Command)

SetParent sets the parent of this command to given command

func (*BaseCommand) SetPositionalArgHelp

func (s *BaseCommand) SetPositionalArgHelp(help string)

SetPositionalArgHelp is a convenience method to set the help text for positional arguments. if help is an empty string, uses just the name of the command.

type CobraCommand

type CobraCommand interface {
	Cobra() *cobra.Command
}

CobraCommand is an interface for commands that can refer back to their base cobra.Command

type Command

type Command interface {
	SetConfig(config *config.Config)
	SetParent(Command)
	SetChild(command Command)
	SetFlags(flags []string) error
	DeleteChild(command Command)
	Children() []Command
	Parent() Command
	Name() string
	InitCommand()
	MakeExecuteCommand() func(args []string) (interface{}, error)
	MakePreExecuteCommand() func(args []string) error
	MakePersistentPreExecuteCommand() func(args []string) error
	SetConfigLoader(func(config *config.Config) error)
	ConfigLoader() func(config *config.Config) error
	Config() *config.Config
	HandleOutput(writer io.Writer, out interface{}) error
	HandleError(err error)
	CobraCommand
}

Command defines the common functionality for all commands

func BuildCommand

func BuildCommand(child, parent Command, config *config.Config) Command

BuildCommand sets up a Command with the specified config and adds it to Cobra

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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