提交 86d2d026 编写于 作者: D Derek Parker

Add basic command implementation

上级 170ae883
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
}
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")
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册