diff options
author | Brian Cully <bjc@kublai.com> | 2015-07-10 20:16:11 +0000 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2015-07-10 20:16:11 +0000 |
commit | 16815048d2f7f770a454597a39838e63014132f2 (patch) | |
tree | 57d843693a510697ea5bb2487eea07570e48be03 | |
parent | f8f30593d7df521d45fd85e1d90fb7b7fe54a980 (diff) | |
download | goctl-16815048d2f7f770a454597a39838e63014132f2.tar.gz goctl-16815048d2f7f770a454597a39838e63014132f2.zip |
Add builtin 'help' command.
Allows handlers to output helpful information.
-rw-r--r-- | cmd_help.go | 32 | ||||
-rw-r--r-- | cmd_help_test.go | 29 | ||||
-rw-r--r-- | cmd_pid.go | 4 | ||||
-rw-r--r-- | cmd_ping.go | 4 | ||||
-rw-r--r-- | goctl.go | 1 | ||||
-rw-r--r-- | goctl_test.go | 4 |
6 files changed, 74 insertions, 0 deletions
diff --git a/cmd_help.go b/cmd_help.go new file mode 100644 index 0000000..b14471c --- /dev/null +++ b/cmd_help.go @@ -0,0 +1,32 @@ +package goctl + +import ( + "fmt" + "sort" + "strings" +) + +type cmdHelp struct{} + +func (cmd cmdHelp) Name() string { + return "help" +} + +func (cmd cmdHelp) Help() string { + return "show this message" +} + +func (cmd cmdHelp) Run(gc *Goctl, args []string) string { + rc := []string{"Available commands:", ""} + cmds := []string{} + for _, h := range gc.handlers { + cmds = append(cmds, fmt.Sprintf("\t%s\t%s", h.Name(), h.Help())) + } + sort.Strings(cmds) + rc = append(rc, cmds...) + return strings.Join(rc, "\n") +} + +func init() { + builtinHandlers = append(builtinHandlers, cmdHelp{}) +} diff --git a/cmd_help_test.go b/cmd_help_test.go new file mode 100644 index 0000000..9c9a930 --- /dev/null +++ b/cmd_help_test.go @@ -0,0 +1,29 @@ +package goctl + +import "testing" + +func TestHelp(t *testing.T) { + gc := start(t) + defer gc.Stop() + + c := dial(t) + defer c.Close() + + buf := []byte("help") + Write(c, buf) + + buf, err := Read(c) + if err != nil { + t.Fatalf("Couldn't read from socket: %s.", err) + } + + got := string(buf) + want := `Available commands: + + help show this message + pid return the Unix process ID of this program + ping checks whether the connection is working` + if got != want { + t.Errorf("Didn't get proper help response.\nGot:\n'%s',\nWant:\n'%s'", got, want) + } +} @@ -13,6 +13,10 @@ func (cmd cmdPID) Name() string { return "pid" } +func (cmd cmdPID) Help() string { + return "return the Unix process ID of this program" +} + func (cmd cmdPID) Run(_ *Goctl, _ []string) string { return pid } diff --git a/cmd_ping.go b/cmd_ping.go index c052649..48294ea 100644 --- a/cmd_ping.go +++ b/cmd_ping.go @@ -6,6 +6,10 @@ func (cmd *cmdPing) Name() string { return "ping" } +func (cmd cmdPing) Help() string { + return "checks whether the connection is working" +} + func (cmd *cmdPing) Run(_ *Goctl, _ []string) string { return "pong" } @@ -36,6 +36,7 @@ type Goctl struct { type Handler interface { Name() string + Help() string Run(*Goctl, []string) string } diff --git a/goctl_test.go b/goctl_test.go index dce75e7..1ab0238 100644 --- a/goctl_test.go +++ b/goctl_test.go @@ -22,6 +22,10 @@ func (th testHandler) Name() string { return th.name } +func (th testHandler) Help() string { + return "" +} + func (th testHandler) Run(gc *Goctl, args []string) string { return th.fn(gc, args) } |