packer-cn/packer/plugin/hook.go

38 lines
603 B
Go
Raw Normal View History

2013-05-11 13:46:17 -04:00
package plugin
import (
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2013-05-11 13:46:17 -04:00
"log"
)
type cmdHook struct {
2013-05-20 19:50:35 -04:00
hook packer.Hook
client *Client
2013-05-11 13:46:17 -04:00
}
func (c *cmdHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error {
2013-05-11 13:46:17 -04:00
defer func() {
r := recover()
c.checkExit(r, nil)
}()
return c.hook.Run(name, ui, comm, data)
2013-05-11 13:46:17 -04:00
}
2013-08-30 20:03:55 -04:00
func (c *cmdHook) Cancel() {
defer func() {
r := recover()
c.checkExit(r, nil)
}()
c.hook.Cancel()
}
2013-05-11 13:46:17 -04:00
func (c *cmdHook) checkExit(p interface{}, cb func()) {
2013-08-30 20:03:55 -04:00
if c.client.Exited() && cb != nil {
2013-05-11 13:46:17 -04:00
cb()
} else if p != nil && !Killed {
2013-05-11 13:46:17 -04:00
log.Panic(p)
}
}