delve/command/command.go

65 lines
1.3 KiB
Go
Raw Normal View History

2014-05-21 15:23:14 +00:00
// Package command implements functions for responding to user
// input and dispatching to appropriate backend commands.
2014-05-20 21:28:24 +00:00
package command
import (
"fmt"
"os"
)
2014-05-27 15:44:29 +00:00
type cmdfunc func(args ...string) error
2014-05-20 21:28:24 +00:00
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-21 15:23:14 +00:00
// Register custom commands. Expects cf to be a func of type cmdfunc,
// returning only an error.
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
}
2014-05-27 15:44:29 +00:00
func CommandFunc(fn func() error) cmdfunc {
return func(args ...string) error {
return fn()
}
}
func noCmdAvailable(args ...string) error {
2014-05-20 21:28:24 +00:00
return fmt.Errorf("command not available")
}
2014-05-27 15:44:29 +00:00
func exitFunc(args ...string) error {
2014-05-20 21:28:24 +00:00
os.Exit(0)
return nil
}
2014-05-27 15:44:29 +00:00
func nullCommand(args ...string) error {
return nil
}