summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2015-07-10 19:42:00 +0000
committerBrian Cully <bjc@kublai.com>2015-07-10 19:42:00 +0000
commitf38c11939e83895411dfc0d1c79bca172c56f3fc (patch)
treefdbf97491ced825befcaed7c824cd8d3f5331f2d
parent2fd292d5f480fd51cdbbff4821b6f09940a005a9 (diff)
downloadgoctl-f38c11939e83895411dfc0d1c79bca172c56f3fc.tar.gz
goctl-f38c11939e83895411dfc0d1c79bca172c56f3fc.zip
Pass Goctl object into handlers.
-rw-r--r--cmd_pid.go2
-rw-r--r--cmd_ping.go2
-rw-r--r--goctl.go4
-rw-r--r--goctl_test.go27
4 files changed, 19 insertions, 16 deletions
diff --git a/cmd_pid.go b/cmd_pid.go
index 00f127f..d7fd3fe 100644
--- a/cmd_pid.go
+++ b/cmd_pid.go
@@ -13,7 +13,7 @@ func (cmd cmdPID) Name() string {
return "pid"
}
-func (cmd cmdPID) Run(_ []string) string {
+func (cmd cmdPID) Run(_ *Goctl, _ []string) string {
return pid
}
diff --git a/cmd_ping.go b/cmd_ping.go
index 9da8d96..c052649 100644
--- a/cmd_ping.go
+++ b/cmd_ping.go
@@ -6,7 +6,7 @@ func (cmd *cmdPing) Name() string {
return "ping"
}
-func (cmd *cmdPing) Run(_ []string) string {
+func (cmd *cmdPing) Run(_ *Goctl, _ []string) string {
return "pong"
}
diff --git a/goctl.go b/goctl.go
index a94122a..b68734a 100644
--- a/goctl.go
+++ b/goctl.go
@@ -36,7 +36,7 @@ type Goctl struct {
type Handler interface {
Name() string
- Run([]string) string
+ Run(*Goctl, []string) string
}
func init() {
@@ -158,7 +158,7 @@ func (gc *Goctl) reader(c io.ReadWriteCloser) error {
gc.logger.Debug("Got command.", "cmd", cmd)
var resp string
if h := gc.handlers[cmd[0]]; h != nil {
- resp = h.Run(cmd[1:])
+ resp = h.Run(gc, cmd[1:])
} else {
resp = fmt.Sprintf("ERROR: unknown command: '%s'.", cmd[0])
}
diff --git a/goctl_test.go b/goctl_test.go
index a4a1041..7e30f7e 100644
--- a/goctl_test.go
+++ b/goctl_test.go
@@ -11,10 +11,10 @@ import (
type testHandler struct {
name string
- fn func([]string) string
+ fn func(*Goctl, []string) string
}
-func makeHandler(name string, fn func(_ []string) string) testHandler {
+func makeHandler(name string, fn func(_ *Goctl, _ []string) string) testHandler {
return testHandler{name: name, fn: fn}
}
@@ -22,8 +22,8 @@ func (th testHandler) Name() string {
return th.name
}
-func (th testHandler) Run(args []string) string {
- return th.fn(args)
+func (th testHandler) Run(gc *Goctl, args []string) string {
+ return th.fn(gc, args)
}
var sockpath string
@@ -39,12 +39,12 @@ func init() {
}
}
-func start(t testing.TB) Goctl {
+func start(t testing.TB) *Goctl {
gc := NewGoctl(sockpath)
if err := gc.Start(); err != nil {
t.Fatalf("Couldn't start: %s.", err)
}
- return gc
+ return &gc
}
func dial(t testing.TB) net.Conn {
@@ -115,7 +115,10 @@ func TestAddHandler(t *testing.T) {
gc := start(t)
defer gc.Stop()
- gc.AddHandler(makeHandler("foo", func(args []string) string {
+ gc.AddHandler(makeHandler("foo", func(innerGC *Goctl, args []string) string {
+ if innerGC != gc {
+ t.Errorf("Goctl object not passed into handler properly (got: %p, want: %p).", innerGC, gc)
+ }
if !reflect.DeepEqual(args, []string{"bar", "baz"}) {
t.Errorf("Got %v, expected ['bar', 'baz']", args)
}
@@ -139,13 +142,13 @@ func TestAddHandlers(t *testing.T) {
defer gc.Stop()
gc.AddHandlers([]Handler{
- makeHandler("foo", func(args []string) string {
+ makeHandler("foo", func(_ *Goctl, args []string) string {
if !reflect.DeepEqual(args, []string{"bar", "baz"}) {
t.Errorf("Got %v, expected ['bar', 'baz']", args)
}
return ""
}),
- makeHandler("bar", func(args []string) string {
+ makeHandler("bar", func(_ *Goctl, args []string) string {
if !reflect.DeepEqual(args, []string{"baz", "pham"}) {
t.Errorf("Got %v, expected ['baz', 'pham']", args)
}
@@ -176,15 +179,15 @@ func TestCannotOverrideExtantHandlers(t *testing.T) {
gc := start(t)
defer gc.Stop()
- err := gc.AddHandler(makeHandler("ping", func(args []string) string {
+ err := gc.AddHandler(makeHandler("ping", func(_ *Goctl, args []string) string {
return "gnip"
}))
if err != HandlerExists {
t.Error("Was able to override built-in ping handler.")
}
err = gc.AddHandlers([]Handler{
- makeHandler("foo", func(args []string) string { return "foo" }),
- makeHandler("foo", func(args []string) string { return "foo" }),
+ makeHandler("foo", func(_ *Goctl, args []string) string { return "foo" }),
+ makeHandler("foo", func(_ *Goctl, args []string) string { return "foo" }),
})
if err != HandlerExists {
t.Error("Was able to assign two handlers for 'foo'.")