shell

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrHelp is the error returned if the -help or -h flag is invoked but no such flag is defined.
	ErrHelp error = flag.ErrHelp
)

Functions

This section is empty.

Types

type DefaultFlagSet

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

DefaultFlagSet is the basic FlagSet implementation using the standard golang flag library

func NewDefaultFlagSet

func NewDefaultFlagSet() *DefaultFlagSet

NewDefaultFlagSet returns a new DefaultFlagSet.

func NewDefaultFlagSetWithBase

func NewDefaultFlagSetWithBase(flagSet *flag.FlagSet) *DefaultFlagSet

NewDefaultFlagSetWithBase returns a new DefaultFlagSet using the specified flag.FlagSet as a base.

func (*DefaultFlagSet) Bool

func (flagSet *DefaultFlagSet) Bool(name string, defaultValue bool, usage string)

Bool defines a bool flag with specified name, default value, and usage string.

func (*DefaultFlagSet) Duration

func (flagSet *DefaultFlagSet) Duration(name string, defaultValue time.Duration, usage string)

Duration defines a time.Duration flag with specified name, default value, and usage string.

func (*DefaultFlagSet) Float

func (flagSet *DefaultFlagSet) Float(name string, defaultValue float64, usage string)

Float defines a float64 flag with specified name, default value, and usage string.

func (*DefaultFlagSet) Get

func (flagSet *DefaultFlagSet) Get(name string) interface{}

Get returns the value of the named flag.

func (*DefaultFlagSet) GetBool

func (flagSet *DefaultFlagSet) GetBool(name string) *bool

GetBool returns the value of a named flag as a bool.

func (*DefaultFlagSet) GetDuration

func (flagSet *DefaultFlagSet) GetDuration(name string) *time.Duration

GetDuration returns the value of a named flag as a time.Duration.

func (*DefaultFlagSet) GetFloat

func (flagSet *DefaultFlagSet) GetFloat(name string) *float64

GetFloat returns the value of a named flag as a float64.

func (*DefaultFlagSet) GetInt

func (flagSet *DefaultFlagSet) GetInt(name string) *int64

GetInt returns the value of a named flag as a int64.

func (*DefaultFlagSet) GetString

func (flagSet *DefaultFlagSet) GetString(name string) *string

GetString returns the value of a named flag as a string.

func (*DefaultFlagSet) GetUint

func (flagSet *DefaultFlagSet) GetUint(name string) *uint64

GetUint returns the value of a named flag as a uint64.

func (*DefaultFlagSet) Int

func (flagSet *DefaultFlagSet) Int(name string, defaultValue int64, usage string)

Int defines a int64 flag with specified name, default value, and usage string.

func (*DefaultFlagSet) Parse

func (flagSet *DefaultFlagSet) Parse(args []string) ([]string, error)

Parse parses flag definitions from the argument list, which should not include the command name, and return remaining, non-flag, arguments. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help was set but not defined.

func (*DefaultFlagSet) Parsed

func (flagSet *DefaultFlagSet) Parsed() bool

Parsed returns true if Parse has been called.

func (*DefaultFlagSet) Set

func (flagSet *DefaultFlagSet) Set(name, value string) error

Set sets the value of the named flag.

func (*DefaultFlagSet) String

func (flagSet *DefaultFlagSet) String(name string, defaultValue string, usage string)

String defines a string flag with specified name, default value, and usage string.

func (*DefaultFlagSet) SubFlagSet

func (flagSet *DefaultFlagSet) SubFlagSet(name string) FlagSet

SubFlagSet creates a new flagset that will be used by sub routers and sub commands.

func (*DefaultFlagSet) Uint

func (flagSet *DefaultFlagSet) Uint(name string, defaultValue uint64, usage string)

Uint defines a unit64 flag with specified name, default value, and usage string.

type FlagDefiner

type FlagDefiner interface {
	// Bool defines a bool flag with specified name, default value, and usage string.
	Bool(name string, defaultValue bool, usage string)
	// Int defines a int64 flag with specified name, default value, and usage string.
	Int(name string, defaultValue int64, usage string)
	// Uint defines a unit64 flag with specified name, default value, and usage string.
	Uint(name string, defaultValue uint64, usage string)
	// String defines a string flag with specified name, default value, and usage string.
	String(name string, defaultValue string, usage string)
	// Float defines a float64 flag with specified name, default value, and usage string.
	Float(name string, defaultValue float64, usage string)
	// Duration defines a time.Duration flag with specified name, default value, and usage string.
	Duration(name string, defaultValue time.Duration, usage string)
}

FlagDefiner allows you to define the flags managed by the flag set

type FlagHandler

type FlagHandler interface {
	// Define allows the function to define command-line
	Define(FlagDefiner)
}

FlagHandler allows shell handlers to define additional

type FlagHandlerFunction

type FlagHandlerFunction func(FlagDefiner)

The FlagHandlerFunction type is an adapter to allow the use of ordinary functions as shell flag handlers.

func (FlagHandlerFunction) Define

func (handler FlagHandlerFunction) Define(flagDefiner FlagDefiner)

Define allows the function to define command-line

type FlagSet

type FlagSet interface {
	FlagDefiner
	FlagValues

	// SubFlagSet creates a new flagset that will be used by sub routers and sub commands.
	SubFlagSet(name string) FlagSet

	// Parse parses flag definitions from the argument list, which should not include the command name, and return remaining, non-flag, arguments.
	// Must be called after all flags in the FlagSet are defined and before flags are accessed by the program.
	// The return value will be ErrHelp if -help was set but not defined.
	Parse(args []string) ([]string, error)
	// Parsed returns true if Parse has been called.
	Parsed() bool
}

A FlagSet represents a set of defined flags

type FlagValues

type FlagValues interface {
	// Get returns the value of the named flag.
	Get(name string) interface{}
	// GetBool returns the value of a named flag as a bool.
	GetBool(name string) *bool
	// GetInt returns the value of a named flag as a int64.
	GetInt(name string) *int64
	// GetUint returns the value of a named flag as a uint64.
	GetUint(name string) *uint64
	// GetString returns the value of a named flag as a string.
	GetString(name string) *string
	// GetFloat returns the value of a named flag as a float64.
	GetFloat(name string) *float64
	// GetDuration returns the value of a named flag as a time.Duration.
	GetDuration(name string) *time.Duration
	// Set sets the value of the named flag.
	Set(name, value string) error
}

FlagValues allows you to retreive flags

type Handler

type Handler interface {
	// Execute is used to execute the shell handler.
	Execute(ResponseWriter, *Request) error
}

The Handler interface describes a shell handler functions.

type HandlerFunction

type HandlerFunction func(ResponseWriter, *Request) error

The HandlerFunction type is an adapter to allow the use of ordinary functions as shell handlers.

func (HandlerFunction) Execute

func (fn HandlerFunction) Execute(writer ResponseWriter, request *Request) error

Execute is used to execute the shell handler function.

type Middleware

type Middleware interface {
	// Handle is used to perform the middleware function.
	Handle(next Handler) Handler
}

The Middleware interface describes a shell middleware function

type MiddlewareFunction

type MiddlewareFunction func(next Handler) Handler

The MiddlewareFunction type is an adapter to allow the use of ordinary functions as shell middleware.

func (MiddlewareFunction) Handle

func (middleware MiddlewareFunction) Handle(next Handler) Handler

Handle is used to perform the middleware function.

type Option

type Option interface {
	// Apply is used to apply the shell options.
	Apply(shell *Shell) error
}

The Option interface describes a shell option function

func OptionErrorWriter

func OptionErrorWriter(writer io.Writer) Option

OptionErrorWriter shell option allows the user to set the shell error writer.

func OptionFlagSet

func OptionFlagSet(flagSet FlagSet) Option

OptionFlagSet shell option allows the user to set the FlagSet used by the shell.

func OptionInput

func OptionInput(reader io.Reader) Option

OptionInput shell option allows the user to set the shell input reader.

func OptionOutputWriter

func OptionOutputWriter(writer io.Writer) Option

OptionOutputWriter shell option allows the user to set the shell output writer.

func OptionShellPrompt

func OptionShellPrompt(prompt string) Option

OptionShellPrompt shell option allows the user to set the basic shell prompt message.

type OptionFunction

type OptionFunction func(shell *Shell) error

The OptionFunction type is an adapter to allow the use of ordinary functions as shell options.

func (OptionFunction) Apply

func (option OptionFunction) Apply(shell *Shell) error

Apply is used to apply the shell options.

type Request

type Request struct {

	// Args contains the arguments passed as part of the request.
	Args []string
	// Flagset contains the flagset used to parse arguments.
	FlagSet FlagSet
	// Path contains the request path.
	Path []string
	// Routes contains the router routes functions linked to the executed router.
	Routes Routes
	// contains filtered or unexported fields
}

A Request represents a the request sent by the shell and processed by the router and handlers.

func NewRequest

func NewRequest(path, args []string, flagSet FlagSet, routes Routes) *Request

NewRequest wraps NewRequestWithContext using context.Background.

func NewRequestWithContext

func NewRequestWithContext(ctx context.Context, path, args []string, flagSet FlagSet, routes Routes) *Request

NewRequestWithContext returns a new Request given a path, args, and routes.

func (*Request) Context

func (request *Request) Context() context.Context

Context returns the request's context. To change the context, use WithContext.

func (*Request) FlagValues

func (request *Request) FlagValues() FlagValues

FlagValues returns the parsed flag values for the request flagset.

func (*Request) UpdateRequest

func (request *Request) UpdateRequest(selectedRoute string, args []string, flagSet FlagSet, routes Routes) *Request

UpdateRequest returns a shallow copy of the request with updated path, args, flagset, and routes.

func (*Request) WithContext

func (request *Request) WithContext(ctx context.Context) *Request

WithContext returns a shallow copy of the request with its context changed to ctx.

type ResponseWriter

type ResponseWriter interface {
	// ErrorWriter returns an io.Writer used for the error output
	ErrorWriter() io.Writer
	// Write byte array to the output writer.
	Write([]byte) (int, error)
	// WriteError will write byte array to the error writer.
	WriteError([]byte) (int, error)
}

ResponseWriter is an interface is used by an shell handler to construct a response.

type Router

type Router interface {
	Handler
	Routes
	// Flags adds a FlagHandler that will add flags to the request FlagSet before
	// it attempts to match a command.
	Flags(FlagHandler)
	// Group adds a new inline-router to the router stack.
	Group(func(r Router)) Router
	// Handle adds a shell handler to the router stack, along the specified command path.
	Handle(string, Handler)
	// HandleFunction adds a shell handler function to the router stack, along the specified command path.
	HandleFunction(string, HandlerFunction)
	// Route adds a new sub-router to the router stack, along the specified command path.
	NotFound(Handler)
	// Use appends one or more middleware onto the router stack.
	Route(string, func(r Router)) Router
	// NotFound defines a shell handler that will respond if a command path cannot be evaluated.
	Use(...Middleware)
}

The Router interface details the core shell router functions.

type Routes

type Routes interface {
	// Routes returns the linked shell handlers.
	Routes() map[string]Handler
	// Middlewares returns the list of middlewares in use by the router.
	Middlewares() []Middleware
	// Match evaluates the routing tree for a handler that matches the supplied arguments
	// and returns the handler, wrapped in the appropriate middleware handler functions
	Match([]string) (Handler, bool)
}

Routes interface describes functions for router traversal.

type Shell

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

Shell exposes the command-line or interactive shell functionality.

The shell can be execute as a command-line tool by using the Execute function or to be run as an interactive shell using the Start function

func (*Shell) Closed

func (shell *Shell) Closed() chan struct{}

Closed is used to determine if the shell session is closed.

func (*Shell) Execute

func (shell *Shell) Execute(ctx context.Context) error

Execute is used to execute the shell, using os.Args to evaluate which function to execute.

func (*Shell) Flags

func (shell *Shell) Flags(fn FlagHandler)

Flags adds a FlagHandler that will add flags to the request FlagSet before it attempts to match a command.

func (*Shell) Group

func (shell *Shell) Group(fn func(r Router)) Router

Group adds a new inline-router to the router stack.

func (*Shell) Handle

func (shell *Shell) Handle(command string, handler Handler)

Handle adds a shell handler to the router stack, along the specified command path.

func (*Shell) HandleFunction

func (shell *Shell) HandleFunction(command string, fn HandlerFunction)

HandleFunction adds a shell handler function to the router stack, along the specified command path.

func (*Shell) NotFound

func (shell *Shell) NotFound(handler Handler)

NotFound defines a shell handler that will respond if a command path cannot be evaluated.

func (*Shell) Options

func (shell *Shell) Options(options ...Option) error

Options will apply the supplied options to the shell.

Options should be called before adding middleware, groups, or handlers.

func (*Shell) Route

func (shell *Shell) Route(command string, fn func(r Router)) Router

Route adds a new sub-router to the router stack, along the specified command path.

func (*Shell) Start

func (shell *Shell) Start(ctx context.Context) error

Start is used to begin a new shell session.

The interactive shell will read input and evaluate the commands to execute handler functions.

func (*Shell) Use

func (shell *Shell) Use(middleware ...Middleware)

Use appends one or more middleware onto the router stack.

type WrapperWriter

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

The WrapperWriter is used as a simple ResponseWritter

func NewWrapperWriter

func NewWrapperWriter(ctx context.Context, outputWritter io.Writer, errorWriter io.Writer) *WrapperWriter

NewWrapperWriter returns a new WrapperWriter using the specified output and error writers.

func (*WrapperWriter) ErrorWriter

func (writer *WrapperWriter) ErrorWriter() io.Writer

ErrorWriter returns an io.Writer used for the error output

func (*WrapperWriter) Write

func (writer *WrapperWriter) Write(bytes []byte) (int, error)

Write byte array to the output writer.

func (*WrapperWriter) WriteError

func (writer *WrapperWriter) WriteError(bytes []byte) (int, error)

WriteError will write byte array to the error writer.

Jump to

Keyboard shortcuts

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