packer: Add Communicator to Hook arguments
This commit is contained in:
parent
7fdb53f5d1
commit
7475ee8377
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue