packer: Add Communicator to Hook arguments

This commit is contained in:
Mitchell Hashimoto 2013-05-12 17:30:30 -07:00
parent 7fdb53f5d1
commit 7475ee8377
7 changed files with 17 additions and 14 deletions

View File

@ -9,7 +9,7 @@ package packer
// in. In addition to that, the Hook is given access to a UI so that it can // in. In addition to that, the Hook is given access to a UI so that it can
// output things to the user. // output things to the user.
type Hook interface { type Hook interface {
Run(string, interface{}, Ui) Run(string, Ui, Communicator, interface{})
} }
// A Hook implementation that dispatches based on an internal mapping. // 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 // 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 // hooks if a mapping exists. If a mapping doesn't exist, then nothing
// happens. // 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] hooks, ok := h.Mapping[name]
if !ok { if !ok {
// No hooks for that name. No problem. // 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 { for _, hook := range hooks {
hook.Run(name, data, ui) hook.Run(name, ui, comm, data)
} }
} }

View File

@ -7,13 +7,15 @@ import (
type TestHook struct { type TestHook struct {
runCalled bool runCalled bool
runComm Communicator
runData interface{} runData interface{}
runName string runName string
runUi Ui 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.runCalled = true
t.runComm = comm
t.runData = data t.runData = data
t.runName = name t.runName = name
t.runUi = ui t.runUi = ui
@ -31,7 +33,7 @@ func TestDispatchHook_Implements(t *testing.T) {
func TestDispatchHook_Run_NoHooks(t *testing.T) { func TestDispatchHook_Run_NoHooks(t *testing.T) {
// Just make sure nothing blows up // Just make sure nothing blows up
dh := &DispatchHook{make(map[string][]Hook)} dh := &DispatchHook{make(map[string][]Hook)}
dh.Run("foo", nil, nil) dh.Run("foo", nil, nil, nil)
} }
func TestDispatchHook_Run(t *testing.T) { func TestDispatchHook_Run(t *testing.T) {
@ -42,7 +44,7 @@ func TestDispatchHook_Run(t *testing.T) {
mapping := make(map[string][]Hook) mapping := make(map[string][]Hook)
mapping["foo"] = []Hook{hook} mapping["foo"] = []Hook{hook}
dh := &DispatchHook{mapping} dh := &DispatchHook{mapping}
dh.Run("foo", 42, nil) dh.Run("foo", nil, nil, 42)
assert.True(hook.runCalled, "run should be called") assert.True(hook.runCalled, "run should be called")
assert.Equal(hook.runName, "foo", "should be proper event") assert.Equal(hook.runName, "foo", "should be proper event")

View File

@ -13,13 +13,13 @@ type cmdHook struct {
client *client 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() { defer func() {
r := recover() r := recover()
c.checkExit(r, nil) 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()) { func (c *cmdHook) checkExit(p interface{}, cb func()) {

View File

@ -9,7 +9,7 @@ import (
type helperHook byte 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) { func TestHook_NoExist(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)

View File

@ -56,7 +56,7 @@ func TestBuilderRPC(t *testing.T) {
assert.True(b.runCalled, "runs hould be called") assert.True(b.runCalled, "runs hould be called")
if b.runCalled { if b.runCalled {
b.runHook.Run("foo", nil, nil) b.runHook.Run("foo", nil, nil, nil)
assert.True(hook.runCalled, "run should be called") assert.True(hook.runCalled, "run should be called")
b.runUi.Say("format") b.runUi.Say("format")

View File

@ -27,8 +27,9 @@ func Hook(client *rpc.Client) *hook {
return &hook{client} 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() server := rpc.NewServer()
RegisterCommunicator(server, comm)
RegisterUi(server, ui) RegisterUi(server, ui)
address := serveSingleConn(server) address := serveSingleConn(server)
@ -43,7 +44,7 @@ func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error {
return err return err
} }
h.hook.Run(args.Name, args.Data, &Ui{client}) h.hook.Run(args.Name, &Ui{client}, Communicator(client), args.Data)
*reply = nil *reply = nil
return nil return nil

View File

@ -12,7 +12,7 @@ type testHook struct {
runUi packer.Ui 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 h.runCalled = true
} }
@ -35,7 +35,7 @@ func TestHookRPC(t *testing.T) {
// Test Run // Test Run
ui := &testUi{} ui := &testUi{}
hClient.Run("foo", 42, ui) hClient.Run("foo", ui, nil, 42)
assert.True(h.runCalled, "run should be called") assert.True(h.runCalled, "run should be called")
} }