diff options
author | Brian Cully <bjc@kublai.com> | 2015-07-10 19:42:00 +0000 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2015-07-10 19:42:00 +0000 |
commit | f38c11939e83895411dfc0d1c79bca172c56f3fc (patch) | |
tree | fdbf97491ced825befcaed7c824cd8d3f5331f2d | |
parent | 2fd292d5f480fd51cdbbff4821b6f09940a005a9 (diff) | |
download | goctl-f38c11939e83895411dfc0d1c79bca172c56f3fc.tar.gz goctl-f38c11939e83895411dfc0d1c79bca172c56f3fc.zip |
Pass Goctl object into handlers.
-rw-r--r-- | cmd_pid.go | 2 | ||||
-rw-r--r-- | cmd_ping.go | 2 | ||||
-rw-r--r-- | goctl.go | 4 | ||||
-rw-r--r-- | goctl_test.go | 27 |
4 files changed, 19 insertions, 16 deletions
@@ -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" } @@ -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'.") |