Add ability to register commands
This commit is contained in:
parent
84f80fd149
commit
8f5190cbef
@ -19,6 +19,10 @@ func DebugCommands() *Commands {
|
|||||||
return &Commands{cmds}
|
return &Commands{cmds}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Commands) Register(cmdstr string, cf cmdfunc) {
|
||||||
|
c.cmds[cmdstr] = cf
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Commands) Find(cmdstr string) cmdfunc {
|
func (c *Commands) Find(cmdstr string) cmdfunc {
|
||||||
cmd, ok := c.cmds[cmdstr]
|
cmd, ok := c.cmds[cmdstr]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package command
|
package command
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestCommandDefault(t *testing.T) {
|
func TestCommandDefault(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
@ -17,3 +20,20 @@ func TestCommandDefault(t *testing.T) {
|
|||||||
t.Fatal("wrong command output")
|
t.Fatal("wrong command output")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCommandRegister(t *testing.T) {
|
||||||
|
cmds := Commands{make(map[string]cmdfunc)}
|
||||||
|
|
||||||
|
cmds.Register("foo", func() error { return fmt.Errorf("registered command") })
|
||||||
|
|
||||||
|
cmd := cmds.Find("foo")
|
||||||
|
|
||||||
|
err := cmd()
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("cmd was not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err.Error() != "registered command" {
|
||||||
|
t.Fatal("wrong command output")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user