gcmd

package module
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2020 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	CommandSymbol         string
	Commands              map[string]Command
	UnknownCommandMessage string
	Store
}

Base is our command base. It keeps track of all commands and their handlers

func New

func New(commandSymbol string) Base

New is the function for creating a new command base. Please use this function instead of creating an instance of Base yourself!

If you really must create a Base yourself, then make sure that CommandSymbol and UnknownCommandMessage are set, and that you initialize the Commands map yourself or else you will run into some issues!

func (*Base) CommandExists

func (base *Base) CommandExists(command string) bool

CommandExists checks if a command is registered. It returns true if it is, false if it isn't. CommandExists compares the Name fields of commands.

func (*Base) Get

func (base *Base) Get(key string) interface{}

Get retrieves an interface from the store map

func (*Base) GetCommand

func (base *Base) GetCommand(command string) (Command, bool)

GetCommand retrieves a Command by it's name field from the command base.

GetCommand returns a Command and a boolean. This boolean is true if a command was found, and false if a command could not be found with the provided name.

func (*Base) HandleCommand

func (base *Base) HandleCommand(input string, store Store) (bool, error)

HandleCommand is called to run what is assumed to be a command.

HandlerCommand returns a boolean and an error. The boolean is true if the input provided was structed as a proper command meaning that it was a string whose first character is equal to this base's CommandSymbol. If it was not a structured command the bool will be false and the returned error will be nil. In this case, you should assume the user was not trying to execute a command.

If the user entered a properly structured command, but it was either an unknown command or if the input failed validation then the bool will be false and error will be set to an error.

If the command was fully executed successfully, the bool will be true and error will be nil.

func (*Base) Register

func (base *Base) Register(command Command) error

Register takes in a command string and a command handler function (type Handler).

CommandExists is used to check if the command is already registered or not. If the command is already registered, an error will be returned.

func (*Base) Set

func (base *Base) Set(key string, value interface{})

Set stores an interface to the store map

type Command

type Command struct {
	Name  string
	Usage string

	Handler HandlerFunc
	// contains filtered or unexported fields
}

Command is a struct which holds the data required for a command

func (*Command) Use added in v0.1.2

func (cmd *Command) Use(middleware ...MiddlewareFunc)

Use adds middleware to the middleware chain

type Context

type Context struct {
	Args    []string
	Store   Store
	Command *Command
}

Context holds context data for use by command handlers

func (*Context) Get

func (c *Context) Get(key string) interface{}

Get retrieves an interface from the context

func (*Context) Set added in v0.1.2

func (c *Context) Set(key string, value interface{})

Set retrieves an interface from the context

type HandlerFunc added in v0.1.2

type HandlerFunc func(c Context) error

HandlerFunc is a type representation of a command handling function. Handler functions should return true if they were executed successfully, and false if they were cancelled.

type MiddlewareFunc added in v0.1.2

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc represents a chainable middleware function

type Store

type Store map[string]interface{}

Store is a string->interface map for storing context data to be passed to handlers

Jump to

Keyboard shortcuts

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