Add basic command implementation
This commit is contained in:
parent
170ae88312
commit
86d2d026dd
38
command/command.go
Normal file
38
command/command.go
Normal file
@ -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
|
||||
}
|
19
command/command_test.go
Normal file
19
command/command_test.go
Normal file
@ -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")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user