From 16815048d2f7f770a454597a39838e63014132f2 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Fri, 10 Jul 2015 20:16:11 +0000 Subject: Add builtin 'help' command. Allows handlers to output helpful information. --- cmd_help.go | 32 ++++++++++++++++++++++++++++++++ cmd_help_test.go | 29 +++++++++++++++++++++++++++++ cmd_pid.go | 4 ++++ cmd_ping.go | 4 ++++ goctl.go | 1 + goctl_test.go | 4 ++++ 6 files changed, 74 insertions(+) create mode 100644 cmd_help.go create mode 100644 cmd_help_test.go 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) + } +} diff --git a/cmd_pid.go b/cmd_pid.go index d7fd3fe..3bdb1f3 100644 --- a/cmd_pid.go +++ b/cmd_pid.go @@ -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" } diff --git a/goctl.go b/goctl.go index b68734a..6379cd9 100644 --- a/goctl.go +++ b/goctl.go @@ -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) } -- cgit v1.2.3