diff --git a/command/command.go b/command/command.go new file mode 100644 index 0000000000000000000000000000000000000000..ef1f08570403a26485e229db74bbfe15d57fee99 --- /dev/null +++ b/command/command.go @@ -0,0 +1,38 @@ +package command + +import ( + "fmt" + "os" +) + +type cmdfunc func() error + +type Commands struct { + cmds map[string]cmdfunc +} + +func DebugCommands() *Commands { + cmds := map[string]cmdfunc{ + "exit": exitFunc, + } + + return &Commands{cmds} +} + +func (c *Commands) Find(cmdstr string) cmdfunc { + cmd, ok := c.cmds[cmdstr] + if !ok { + return noCmdAvailable + } + + return cmd +} + +func noCmdAvailable() error { + return fmt.Errorf("command not available") +} + +func exitFunc() error { + os.Exit(0) + return nil +} diff --git a/command/command_test.go b/command/command_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f6c96e629f77baf92f83afaa27600bdf03ed01d2 --- /dev/null +++ b/command/command_test.go @@ -0,0 +1,19 @@ +package command + +import "testing" + +func TestCommandDefault(t *testing.T) { + var ( + cmds = Commands{make(map[string]cmdfunc)} + cmd = cmds.Find("non-existant-command") + ) + + err := cmd() + if err == nil { + t.Fatal("cmd() did not default") + } + + if err.Error() != "command not available" { + t.Fatal("wrong command output") + } +}