command

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package command provides CLI style interactive commands.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSkipHears suggests that a handler has dealt with a command,
	// and than any subsequent Hears handlers should be skipped.
	ErrSkipHears = errors.New("skip hear messages")

	// ErrUnknownCommand is returned by a command mux if the command did
	// not match any of it's registered handlers.
	ErrUnknownCommand = errors.New("unknown command")

	// ErrBadCLI implies that we could not process this message as a
	// command line. E.g. due to potentially mismatched quoting or bad
	// escaping.
	ErrBadCLI = errors.New("could not process as command line")
)

Functions

func ErrUsage

func ErrUsage(s string) error

ErrUsage indicates that Command handler was used incorrectly. The string returned is a usage message generated by a call to -help for this command

Types

type Command

type Command struct {
	Use     string
	Short   string
	Long    string
	Example string

	PersistentPreRun  RunFunc
	PreRun            RunFunc
	Run               RunFunc
	PostRun           RunFunc
	PersistentPostRun RunFunc
	SilenceErrors     bool
	SilenceUsage      bool
	// contains filtered or unexported fields
}

Command describe a CLI type command that can be executed by a user. It a wrapper around an github.com/spf13/cobra command

func FromContext

func FromContext(ctx context.Context) *Command

FromContext retrieves the Command the caused the calling function be be called

func (*Command) AddCommand

func (cmd *Command) AddCommand(scmd *Command)

AddCommand adds a new subcommand to this command

func (*Command) Flags

func (cmd *Command) Flags() *pflag.FlagSet

Flags returns the active FlagSet for this command

func (*Command) PersistentFlags

func (cmd *Command) PersistentFlags() *pflag.FlagSet

PersistentFlags returns the persistent FlagSet for this command

type Handler

type Handler struct {
	Setupper
}

Handler is responsible for executing a user command. Before the command is run the Setupper is called to build the new command. The command is the executed. This allows the setup stage to capture flag information that is needed when the command itself is execture

func New

func New(f Setupper) *Handler

New creates a handler from any CommnadSetupper

func NewFunc

func NewFunc(f func(cmd *Command) error) *Handler

NewFunc creates a Handler from a SetupFunc

func (*Handler) Describe

func (h *Handler) Describe() (string, string)

Describe implements hugot.Handler for the command handler

func (*Handler) Help

func (h *Handler) Help(w io.Writer) error

Help implements mux.Helper for the command.Handler

func (*Handler) ProcessMessage

func (h *Handler) ProcessMessage(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error

ProcessMessage implements hugot.Handler for the command handler

type RunFunc

type RunFunc func(ctx context.Context, w hugot.ResponseWriter, msg *hugot.Message, args []string) error

RunFunc is a specification for a function to be run in response to a user executing a command. msg is the original message, args is the set of arguments. And flags required should be setup during Setup dunction for the handler

type Set

type Set map[string]*Handler

Set is a collection of command to be run by a mux.Mux

func (Set) Describe

func (cs Set) Describe() (string, string)

Describe implements the hugot.Handler interface for a Set

func (Set) Help

func (cs Set) Help(w io.Writer) error

Help implements mux.Helper for the command.CommandSet

func (Set) MustAdd

func (cs Set) MustAdd(c Setupper)

MustAdd adds a command to a Set

func (Set) ProcessMessage

func (cs Set) ProcessMessage(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error

ProcessMessage implements the hugot.Handler interface for a Set

type SetupFunc

type SetupFunc func(*Command) error

SetupFunc takes a Command and is expected to configure it to provide some command functionality

func (SetupFunc) CommandSetup

func (f SetupFunc) CommandSetup(cmd *Command) error

CommandSetup you to use a CommandSetupFunc directly as a Handler

type Setupper

type Setupper interface {
	CommandSetup(*Command) error
}

Setupper describes a Handler that can set up the context for a user executing a function. It should set up the provided function as required, and ensure the Run functions capture any required flag arguments

Directories

Path Synopsis
Package ping provides a handler that replies to any message sent
Package ping provides a handler that replies to any message sent
Package testcli provides an example Command handler with nested command handling.
Package testcli provides an example Command handler with nested command handling.
Package uptime provides a handler that replies to any message sent
Package uptime provides a handler that replies to any message sent

Jump to

Keyboard shortcuts

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