delve/command/command.go

55 lines
966 B
Go
Raw Normal View History

2014-05-20 21:28:24 +00:00
package command
import (
"fmt"
"os"
)
type cmdfunc func() error
type Commands struct {
cmds map[string]cmdfunc
}
// Returns a Commands struct with default commands defined.
2014-05-20 21:28:24 +00:00
func DebugCommands() *Commands {
cmds := map[string]cmdfunc{
"exit": exitFunc,
"": nullCommand,
2014-05-20 21:28:24 +00:00
}
return &Commands{cmds}
}
2014-05-20 23:09:34 +00:00
func (c *Commands) Register(cmdstr string, cf cmdfunc) {
c.cmds[cmdstr] = cf
}
// Find will look up the command function for the given command input.
// If it cannot find the command it will defualt to noCmdAvailable().
// If the command is an empty string it will replay the last command.
2014-05-20 21:28:24 +00:00
func (c *Commands) Find(cmdstr string) cmdfunc {
cmd, ok := c.cmds[cmdstr]
if !ok {
return noCmdAvailable
}
// Allow <enter> to replay last command
c.cmds[""] = cmd
2014-05-20 21:28:24 +00:00
return cmd
}
func noCmdAvailable() error {
return fmt.Errorf("command not available")
}
func exitFunc() error {
os.Exit(0)
return nil
}
func nullCommand() error {
return nil
}