README ¶
allot
allot is a small Golang
library to match and parse commands with pre-defined strings. For example use allot to define a list of commands your CLI application or Slackbot supports and check if incoming requests are matching your commands.
The allot library supports placeholders and regular expressions for parameter matching and parsing.
Usage
cmd := allot.NewCommand("revert <commits:integer> commits on <project:string> at (stage|prod)")
match, err := cmd.Match("revert 12 commits on example at prod")
if (err != nil)
commits, _ = match.Integer("commits")
project, _ = match.String("project")
env, _ = match.Match(2)
fmt.Printf("Revert \"%d\" on \"%s\" at \"%s\"", commits, project, env)
} else {
fmt.Println("Request did not match command.")
}
Examples
See the hanu Slackbot framework for a usecase for allot:
Credits
Documentation ¶
Index ¶
- func Expression(data string) *regexp.Regexp
- type Command
- func (c Command) Expression() *regexp.Regexp
- func (c Command) Has(param ParameterInterface) bool
- func (c Command) Match(req string) (MatchInterface, error)
- func (c Command) Matches(req string) bool
- func (c Command) Parameters() []Parameter
- func (c Command) Position(param ParameterInterface) int
- func (c Command) Text() string
- type CommandInterface
- type Match
- type MatchInterface
- type Parameter
- type ParameterInterface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Expression ¶
Expression returns the regexp for a data type
Types ¶
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command is a Command definition
func (Command) Expression ¶
Expression returns the regular expression matching the command text
func (Command) Has ¶
func (c Command) Has(param ParameterInterface) bool
Has checks if the parameter is found in the command
func (Command) Match ¶
func (c Command) Match(req string) (MatchInterface, error)
Match returns the parameter matching the expression at the defined position
func (Command) Parameters ¶
Parameters returns the list of defined parameters
func (Command) Position ¶
func (c Command) Position(param ParameterInterface) int
Position returns the position of a parameter
type CommandInterface ¶
type CommandInterface interface { Expression() *regexp.Regexp Has(name ParameterInterface) bool Match(req string) (MatchInterface, error) Matches(req string) bool Parameters() []Parameter Position(param ParameterInterface) int Text() string }
CommandInterface describes how to access a Command
type Match ¶
type Match struct { Command CommandInterface Request string }
Match is the Match definition
type MatchInterface ¶
type MatchInterface interface { String(name string) (string, error) Integer(name string) (int, error) Match(position int) (string, error) Parameter(param ParameterInterface) (string, error) }
MatchInterface describes how to access a Match
type Parameter ¶
type Parameter struct {
// contains filtered or unexported fields
}
Parameter is the Parameter definition
func NewParameterWithType ¶
NewParameterWithType returns
func (Parameter) Equals ¶
func (p Parameter) Equals(param ParameterInterface) bool
Equals checks if two parameter are equal
func (Parameter) Expression ¶
Expression returns the regexp behind the type
type ParameterInterface ¶
type ParameterInterface interface { Equals(param ParameterInterface) bool Expression() *regexp.Regexp Name() string Data() string }
ParameterInterface describes how to access a Parameter