From 7475ee83773e261670e8720a41c5b26e2a9c700e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 12 May 2013 17:30:30 -0700 Subject: [PATCH] packer: Add Communicator to Hook arguments --- packer/hook.go | 6 +++--- packer/hook_test.go | 8 +++++--- packer/plugin/hook.go | 4 ++-- packer/plugin/hook_test.go | 2 +- packer/rpc/builder_test.go | 2 +- packer/rpc/hook.go | 5 +++-- packer/rpc/hook_test.go | 4 ++-- 7 files changed, 17 insertions(+), 14 deletions(-) diff --git a/packer/hook.go b/packer/hook.go index 63cfb98d9..abb50a614 100644 --- a/packer/hook.go +++ b/packer/hook.go @@ -9,7 +9,7 @@ package packer // in. In addition to that, the Hook is given access to a UI so that it can // output things to the user. type Hook interface { - Run(string, interface{}, Ui) + Run(string, Ui, Communicator, interface{}) } // A Hook implementation that dispatches based on an internal mapping. @@ -20,7 +20,7 @@ type DispatchHook struct { // Runs the hook with the given name by dispatching it to the proper // hooks if a mapping exists. If a mapping doesn't exist, then nothing // happens. -func (h *DispatchHook) Run(name string, data interface{}, ui Ui) { +func (h *DispatchHook) Run(name string, ui Ui, comm Communicator, data interface{}) { hooks, ok := h.Mapping[name] if !ok { // No hooks for that name. No problem. @@ -28,6 +28,6 @@ func (h *DispatchHook) Run(name string, data interface{}, ui Ui) { } for _, hook := range hooks { - hook.Run(name, data, ui) + hook.Run(name, ui, comm, data) } } diff --git a/packer/hook_test.go b/packer/hook_test.go index eacd1c90a..550568e25 100644 --- a/packer/hook_test.go +++ b/packer/hook_test.go @@ -7,13 +7,15 @@ import ( type TestHook struct { runCalled bool + runComm Communicator runData interface{} runName string runUi Ui } -func (t *TestHook) Run(name string, data interface{}, ui Ui) { +func (t *TestHook) Run(name string, ui Ui, comm Communicator, data interface{}) { t.runCalled = true + t.runComm = comm t.runData = data t.runName = name t.runUi = ui @@ -31,7 +33,7 @@ func TestDispatchHook_Implements(t *testing.T) { func TestDispatchHook_Run_NoHooks(t *testing.T) { // Just make sure nothing blows up dh := &DispatchHook{make(map[string][]Hook)} - dh.Run("foo", nil, nil) + dh.Run("foo", nil, nil, nil) } func TestDispatchHook_Run(t *testing.T) { @@ -42,7 +44,7 @@ func TestDispatchHook_Run(t *testing.T) { mapping := make(map[string][]Hook) mapping["foo"] = []Hook{hook} dh := &DispatchHook{mapping} - dh.Run("foo", 42, nil) + dh.Run("foo", nil, nil, 42) assert.True(hook.runCalled, "run should be called") assert.Equal(hook.runName, "foo", "should be proper event") diff --git a/packer/plugin/hook.go b/packer/plugin/hook.go index 532b3eea8..8bc1c2ed5 100644 --- a/packer/plugin/hook.go +++ b/packer/plugin/hook.go @@ -13,13 +13,13 @@ type cmdHook struct { client *client } -func (c *cmdHook) Run(name string, data interface{}, ui packer.Ui) { +func (c *cmdHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) { defer func() { r := recover() c.checkExit(r, nil) }() - c.hook.Run(name, data, ui) + c.hook.Run(name, ui, comm, data) } func (c *cmdHook) checkExit(p interface{}, cb func()) { diff --git a/packer/plugin/hook_test.go b/packer/plugin/hook_test.go index 1f3ce25c1..8680be903 100644 --- a/packer/plugin/hook_test.go +++ b/packer/plugin/hook_test.go @@ -9,7 +9,7 @@ import ( type helperHook byte -func (helperHook) Run(string, interface{}, packer.Ui) {} +func (helperHook) Run(string, packer.Ui, packer.Communicator, interface{}) {} func TestHook_NoExist(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index 0584ac0af..07eb26994 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -56,7 +56,7 @@ func TestBuilderRPC(t *testing.T) { assert.True(b.runCalled, "runs hould be called") if b.runCalled { - b.runHook.Run("foo", nil, nil) + b.runHook.Run("foo", nil, nil, nil) assert.True(hook.runCalled, "run should be called") b.runUi.Say("format") diff --git a/packer/rpc/hook.go b/packer/rpc/hook.go index 3d6b5a532..d01b86cfb 100644 --- a/packer/rpc/hook.go +++ b/packer/rpc/hook.go @@ -27,8 +27,9 @@ func Hook(client *rpc.Client) *hook { return &hook{client} } -func (h *hook) Run(name string, data interface{}, ui packer.Ui) { +func (h *hook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) { server := rpc.NewServer() + RegisterCommunicator(server, comm) RegisterUi(server, ui) address := serveSingleConn(server) @@ -43,7 +44,7 @@ func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error { return err } - h.hook.Run(args.Name, args.Data, &Ui{client}) + h.hook.Run(args.Name, &Ui{client}, Communicator(client), args.Data) *reply = nil return nil diff --git a/packer/rpc/hook_test.go b/packer/rpc/hook_test.go index dd8f42aa9..66b16b5d9 100644 --- a/packer/rpc/hook_test.go +++ b/packer/rpc/hook_test.go @@ -12,7 +12,7 @@ type testHook struct { runUi packer.Ui } -func (h *testHook) Run(name string, data interface{}, ui packer.Ui) { +func (h *testHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) { h.runCalled = true } @@ -35,7 +35,7 @@ func TestHookRPC(t *testing.T) { // Test Run ui := &testUi{} - hClient.Run("foo", 42, ui) + hClient.Run("foo", ui, nil, 42) assert.True(h.runCalled, "run should be called") }