From 8f5190cbef5d92db18b45a6f8fcf92bce29a1956 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Tue, 20 May 2014 18:09:34 -0500 Subject: [PATCH] Add ability to register commands --- command/command.go | 4 ++++ command/command_test.go | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/command/command.go b/command/command.go index ef1f0857..f7b8be5c 100644 --- a/command/command.go +++ b/command/command.go @@ -19,6 +19,10 @@ func DebugCommands() *Commands { return &Commands{cmds} } +func (c *Commands) Register(cmdstr string, cf cmdfunc) { + c.cmds[cmdstr] = cf +} + func (c *Commands) Find(cmdstr string) cmdfunc { cmd, ok := c.cmds[cmdstr] if !ok { diff --git a/command/command_test.go b/command/command_test.go index f6c96e62..4ad609d8 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -1,6 +1,9 @@ package command -import "testing" +import ( + "fmt" + "testing" +) func TestCommandDefault(t *testing.T) { var ( @@ -17,3 +20,20 @@ func TestCommandDefault(t *testing.T) { 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") + } +} -- GitLab