github.com/annwntech/go-micro/v2@v2.9.5/agent/command/command.go (about) 1 // Package command is an interface for defining bot commands 2 package command 3 4 var ( 5 // Commmands keyed by golang/regexp patterns 6 // regexp.Match(key, input) is used to match 7 Commands = map[string]Command{} 8 ) 9 10 // Command is the interface for specific named 11 // commands executed via plugins or the bot. 12 type Command interface { 13 // Executes the command with args passed in 14 Exec(args ...string) ([]byte, error) 15 // Usage of the command 16 Usage() string 17 // Description of the command 18 Description() string 19 // Name of the command 20 String() string 21 } 22 23 type cmd struct { 24 name string 25 usage string 26 description string 27 exec func(args ...string) ([]byte, error) 28 } 29 30 func (c *cmd) Description() string { 31 return c.description 32 } 33 34 func (c *cmd) Exec(args ...string) ([]byte, error) { 35 return c.exec(args...) 36 } 37 38 func (c *cmd) Usage() string { 39 return c.usage 40 } 41 42 func (c *cmd) String() string { 43 return c.name 44 } 45 46 // NewCommand helps quickly create a new command 47 func NewCommand(name, usage, description string, exec func(args ...string) ([]byte, error)) Command { 48 return &cmd{ 49 name: name, 50 usage: usage, 51 description: description, 52 exec: exec, 53 } 54 }