command

package module
v0.0.0-...-6ed38a2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 15 Imported by: 3

README

go-commands

Documentation

Index

Constants

View Source
const (
	Default = ""

	ErrNotFound = constError("command not found")
)

Variables

View Source
var (
	// CommandUsageColor is the color in which the
	// command usage will be printed on the screen.
	CommandUsageColor = color.New(color.FgHiCyan)

	// CommandDescriptionColor is the color in which the
	// command usage description will be printed on the screen.
	CommandDescriptionColor = color.New(color.FgCyan)

	ArgNameTag        = "arg"
	ArgDescriptionTag = "desc"

	// TimeFormats used in that order to try parse time strings.
	// If a time format has not time zone part,
	// then the date is returned in the local time zone.
	TimeFormats = []string{
		time.RFC3339Nano,
		time.RFC3339,
		"2006-01-02 15:04:05",
		"2006-01-02 15:04",
		"2006-01-02",
	}
)

Functions

This section is empty.

Types

type Arg

type Arg struct {
	Name        string
	Description string
	Type        reflect.Type
}

type Args

type Args interface {
	NumArgs() int
	Args() []Arg
	ArgTag(index int, tag string) string
	String() string
}

type ArgsDef

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

ArgsDef implements Args

var WithoutArgs ArgsDef

func (*ArgsDef) ArgTag

func (def *ArgsDef) ArgTag(index int, tag string) string

func (*ArgsDef) Args

func (def *ArgsDef) Args() []Arg

func (*ArgsDef) Init

func (def *ArgsDef) Init(outerStructPtr interface{}) error

Init initializes ArgsDef with the reflection data from outerStructPtr wich has to be the address of the struct variable that embedds ArgsDef.

func (*ArgsDef) JSONArgsFunc

func (def *ArgsDef) JSONArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (JSONArgsFunc, error)

func (*ArgsDef) JSONArgsResultValuesFunc

func (def *ArgsDef) JSONArgsResultValuesFunc(commandFunc interface{}) (JSONArgsResultValuesFunc, error)

func (*ArgsDef) MapArgsFunc

func (def *ArgsDef) MapArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (MapArgsFunc, error)

func (*ArgsDef) MapArgsResultValuesFunc

func (def *ArgsDef) MapArgsResultValuesFunc(commandFunc interface{}) (MapArgsResultValuesFunc, error)

func (*ArgsDef) NumArgs

func (def *ArgsDef) NumArgs() int

func (*ArgsDef) String

func (def *ArgsDef) String() string

String implements the fmt.Stringer interface.

func (*ArgsDef) StringArgsFunc

func (def *ArgsDef) StringArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (StringArgsFunc, error)

func (*ArgsDef) StringArgsResultValuesFunc

func (def *ArgsDef) StringArgsResultValuesFunc(commandFunc interface{}) (StringArgsResultValuesFunc, error)

func (*ArgsDef) StringMapArgsFunc

func (def *ArgsDef) StringMapArgsFunc(commandFunc interface{}, resultsHandlers []ResultsHandler) (StringMapArgsFunc, error)

func (*ArgsDef) StringMapArgsResultValuesFunc

func (def *ArgsDef) StringMapArgsResultValuesFunc(commandFunc interface{}) (StringMapArgsResultValuesFunc, error)

type JSONArgsFunc

type JSONArgsFunc func(ctx context.Context, args []byte) error

func GetJSONArgsFunc

func GetJSONArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) (JSONArgsFunc, error)

func MustGetJSONArgsFunc

func MustGetJSONArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) JSONArgsFunc

type JSONArgsResultValuesFunc

type JSONArgsResultValuesFunc func(ctx context.Context, args []byte) ([]reflect.Value, error)

func GetJSONArgsResultValuesFunc

func GetJSONArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) (JSONArgsResultValuesFunc, error)

func MustGetJSONArgsResultValuesFunc

func MustGetJSONArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) JSONArgsResultValuesFunc

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger interface

type MapArgsFunc

type MapArgsFunc func(ctx context.Context, args map[string]interface{}) error

func GetMapArgsFunc

func GetMapArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) (MapArgsFunc, error)

func MustGetMapArgsFunc

func MustGetMapArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) MapArgsFunc

type MapArgsResultValuesFunc

type MapArgsResultValuesFunc func(ctx context.Context, args map[string]interface{}) ([]reflect.Value, error)

func GetMapArgsResultValuesFunc

func GetMapArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) (MapArgsResultValuesFunc, error)

func MustGetMapArgsResultValuesFunc

func MustGetMapArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) MapArgsResultValuesFunc

type PrintlnText

type PrintlnText string

PrintlnText prints a fixed string if a command returns without an error

func (PrintlnText) HandleResults

func (t PrintlnText) HandleResults(args Args, argVals, resultVals []reflect.Value, resultErr error) error

type ResultsHandler

type ResultsHandler interface {
	HandleResults(args Args, argVals, resultVals []reflect.Value, resultErr error) error
}

type ResultsHandlerFunc

type ResultsHandlerFunc func(args Args, argVals, resultVals []reflect.Value, resultErr error) error
var Println ResultsHandlerFunc = func(args Args, argVals, resultVals []reflect.Value, resultErr error) error {
	if resultErr != nil {
		return resultErr
	}
	results, err := resultsToInterfaces(resultVals)
	if err != nil || len(results) == 0 {
		return err
	}
	for _, r := range results {
		_, err = fmt.Println(r)
		if err != nil {
			return err
		}
	}
	return nil
}

Println calls fmt.Println for every result

func LogTo

func LogTo(logger Logger) ResultsHandlerFunc

LogTo calls logger.Printf(fmt.Sprintln(results...))

func LogWithPrefixTo

func LogWithPrefixTo(prefix string, logger Logger) ResultsHandlerFunc

LogWithPrefixTo calls logger.Printf(fmt.Sprintln(results...)) with prefix prepended to the results

func PrintTo

func PrintTo(writer io.Writer) ResultsHandlerFunc

PrintTo calls fmt.Fprint on writer with the result values as varidic arguments

func PrintlnTo

func PrintlnTo(writer io.Writer) ResultsHandlerFunc

PrintlnTo calls fmt.Fprintln on writer for every result

func PrintlnWithPrefix

func PrintlnWithPrefix(prefix string) ResultsHandlerFunc

PrintlnWithPrefix calls fmt.Println(prefix, result) for every result value

func PrintlnWithPrefixTo

func PrintlnWithPrefixTo(prefix string, writer io.Writer) ResultsHandlerFunc

PrintlnWithPrefixTo calls fmt.Fprintln(writer, prefix, result) for every result value

func (ResultsHandlerFunc) HandleResults

func (f ResultsHandlerFunc) HandleResults(args Args, argVals, resultVals []reflect.Value, resultErr error) error

type StringArgsCommandLogger

type StringArgsCommandLogger interface {
	LogStringArgsCommand(command string, args []string)
}

type StringArgsCommandLoggerFunc

type StringArgsCommandLoggerFunc func(command string, args []string)

func (StringArgsCommandLoggerFunc) LogStringArgsCommand

func (f StringArgsCommandLoggerFunc) LogStringArgsCommand(command string, args []string)

type StringArgsDispatcher

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

func NewStringArgsDispatcher

func NewStringArgsDispatcher(loggers ...StringArgsCommandLogger) *StringArgsDispatcher

func (*StringArgsDispatcher) AddCommand

func (disp *StringArgsDispatcher) AddCommand(command, description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler) error

func (*StringArgsDispatcher) AddDefaultCommand

func (disp *StringArgsDispatcher) AddDefaultCommand(description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler) error

func (*StringArgsDispatcher) Dispatch

func (disp *StringArgsDispatcher) Dispatch(ctx context.Context, command string, args ...string) error

func (*StringArgsDispatcher) DispatchCombinedCommandAndArgs

func (disp *StringArgsDispatcher) DispatchCombinedCommandAndArgs(ctx context.Context, commandAndArgs []string) (command string, err error)

func (*StringArgsDispatcher) DispatchDefaultCommand

func (disp *StringArgsDispatcher) DispatchDefaultCommand() error

func (*StringArgsDispatcher) HasCommnd

func (disp *StringArgsDispatcher) HasCommnd(command string) bool

func (*StringArgsDispatcher) HasDefaultCommnd

func (disp *StringArgsDispatcher) HasDefaultCommnd() bool

func (*StringArgsDispatcher) MustAddCommand

func (disp *StringArgsDispatcher) MustAddCommand(command, description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler)

func (*StringArgsDispatcher) MustAddDefaultCommand

func (disp *StringArgsDispatcher) MustAddDefaultCommand(description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler)

func (*StringArgsDispatcher) MustDispatch

func (disp *StringArgsDispatcher) MustDispatch(ctx context.Context, command string, args ...string)

func (*StringArgsDispatcher) MustDispatchCombinedCommandAndArgs

func (disp *StringArgsDispatcher) MustDispatchCombinedCommandAndArgs(ctx context.Context, commandAndArgs []string) (command string)

func (*StringArgsDispatcher) MustDispatchDefaultCommand

func (disp *StringArgsDispatcher) MustDispatchDefaultCommand()

func (*StringArgsDispatcher) PrintCommands

func (disp *StringArgsDispatcher) PrintCommands(appName string)

func (*StringArgsDispatcher) PrintCommandsUsageIntro

func (disp *StringArgsDispatcher) PrintCommandsUsageIntro(appName string, output io.Writer)

type StringArgsFunc

type StringArgsFunc func(ctx context.Context, args ...string) error

func GetStringArgsFunc

func GetStringArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) (StringArgsFunc, error)

func MustGetStringArgsFunc

func MustGetStringArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) StringArgsFunc

type StringArgsResultValuesFunc

type StringArgsResultValuesFunc func(ctx context.Context, args []string) ([]reflect.Value, error)

func GetStringArgsResultValuesFunc

func GetStringArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) (StringArgsResultValuesFunc, error)

func MustGetStringArgsResultValuesFunc

func MustGetStringArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) StringArgsResultValuesFunc

type StringMapArgsFunc

type StringMapArgsFunc func(ctx context.Context, args map[string]string) error

func GetStringMapArgsFunc

func GetStringMapArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) (StringMapArgsFunc, error)

func MustGetStringMapArgsFunc

func MustGetStringMapArgsFunc(commandFunc interface{}, argsStructPtr interface{}, resultsHandlers ...ResultsHandler) StringMapArgsFunc

type StringMapArgsResultValuesFunc

type StringMapArgsResultValuesFunc func(ctx context.Context, args map[string]string) ([]reflect.Value, error)

func GetStringMapArgsResultValuesFunc

func GetStringMapArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) (StringMapArgsResultValuesFunc, error)

func MustGetStringMapArgsResultValuesFunc

func MustGetStringMapArgsResultValuesFunc(commandFunc interface{}, argsStructPtr interface{}) StringMapArgsResultValuesFunc

type SuperCommandNotFound

type SuperCommandNotFound string

func (SuperCommandNotFound) Error

func (s SuperCommandNotFound) Error() string

type SuperStringArgsDispatcher

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

func NewSuperStringArgsDispatcher

func NewSuperStringArgsDispatcher(loggers ...StringArgsCommandLogger) *SuperStringArgsDispatcher

func (*SuperStringArgsDispatcher) AddDefaultCommand

func (disp *SuperStringArgsDispatcher) AddDefaultCommand(description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler) error

func (*SuperStringArgsDispatcher) AddSuperCommand

func (disp *SuperStringArgsDispatcher) AddSuperCommand(superCommand string) (subDisp *StringArgsDispatcher, err error)

func (*SuperStringArgsDispatcher) Dispatch

func (disp *SuperStringArgsDispatcher) Dispatch(ctx context.Context, superCommand, command string, args ...string) error

func (*SuperStringArgsDispatcher) DispatchCombinedCommandAndArgs

func (disp *SuperStringArgsDispatcher) DispatchCombinedCommandAndArgs(ctx context.Context, commandAndArgs []string) (superCommand, command string, err error)

func (*SuperStringArgsDispatcher) DispatchDefaultCommand

func (disp *SuperStringArgsDispatcher) DispatchDefaultCommand() error

func (*SuperStringArgsDispatcher) HasCommnd

func (disp *SuperStringArgsDispatcher) HasCommnd(superCommand string) bool

func (*SuperStringArgsDispatcher) HasSubCommnd

func (disp *SuperStringArgsDispatcher) HasSubCommnd(superCommand, command string) bool

func (*SuperStringArgsDispatcher) MustAddDefaultCommand

func (disp *SuperStringArgsDispatcher) MustAddDefaultCommand(description string, commandFunc interface{}, args Args, resultsHandlers ...ResultsHandler)

func (*SuperStringArgsDispatcher) MustAddSuperCommand

func (disp *SuperStringArgsDispatcher) MustAddSuperCommand(superCommand string) (subDisp *StringArgsDispatcher)

func (*SuperStringArgsDispatcher) MustDispatch

func (disp *SuperStringArgsDispatcher) MustDispatch(ctx context.Context, superCommand, command string, args ...string)

func (*SuperStringArgsDispatcher) MustDispatchCombinedCommandAndArgs

func (disp *SuperStringArgsDispatcher) MustDispatchCombinedCommandAndArgs(ctx context.Context, commandAndArgs []string) (superCommand, command string)

func (*SuperStringArgsDispatcher) MustDispatchDefaultCommand

func (disp *SuperStringArgsDispatcher) MustDispatchDefaultCommand()

func (*SuperStringArgsDispatcher) PrintCommands

func (disp *SuperStringArgsDispatcher) PrintCommands(appName string)

func (*SuperStringArgsDispatcher) PrintCommandsUsageIntro

func (disp *SuperStringArgsDispatcher) PrintCommandsUsageIntro(appName string, output io.Writer)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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