shell

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 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
	// 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, routes Routes) *Request

NewRequest wraps NewRequestWithContext using context.Background.

func NewRequestWithContext

func NewRequestWithContext(ctx context.Context, path, args []string, 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) WithContext

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

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

func (*Request) WithRoutes

func (request *Request) WithRoutes(selectedRoute string, routes Routes) *Request

WithRoutes returns a shallow copy of the request with updated Routes, the selected route added to paths array, and removed from the args array

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
	// Use appends one or more middleware onto the router stack.
	Use(...Middleware)
	// Group adds a new inline-router to the router stack.
	Group(func(r Router)) Router
	// Route adds a new sub-router to the router stack, along the specified command path.
	Route(string, 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)
	// NotFound defines a shell handler that will respond if a command path cannot be evaluated.
	NotFound(Handler)
}

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) 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