summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2015-07-10 20:16:11 +0000
committerBrian Cully <bjc@kublai.com>2015-07-10 20:16:11 +0000
commit16815048d2f7f770a454597a39838e63014132f2 (patch)
tree57d843693a510697ea5bb2487eea07570e48be03
parentf8f30593d7df521d45fd85e1d90fb7b7fe54a980 (diff)
downloadgoctl-16815048d2f7f770a454597a39838e63014132f2.tar.gz
goctl-16815048d2f7f770a454597a39838e63014132f2.zip
Add builtin 'help' command.
Allows handlers to output helpful information.
-rw-r--r--cmd_help.go32
-rw-r--r--cmd_help_test.go29
-rw-r--r--cmd_pid.go4
-rw-r--r--cmd_ping.go4
-rw-r--r--goctl.go1
-rw-r--r--goctl_test.go4
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)
+ }
+}
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)
}