builder/common: multistep debug fn gracefully exits during an interrupt

This commit is contained in:
Mitchell Hashimoto 2013-06-14 15:47:06 -07:00
parent ac029d9ed0
commit 21b6d2a435
4 changed files with 21 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"time"
)
// MultistepDebugFn will return a proper multistep.DebugPauseFn to
@ -21,10 +22,23 @@ func MultistepDebugFn(ui packer.Ui) multistep.DebugPauseFn {
}
message := fmt.Sprintf(
"Pausing %s step '%s'. Press any key to continue.\n",
"Pausing %s step '%s'. Press any key to continue.",
locationString, name)
ui.Say(message)
ui.Ask("")
result := make(chan string, 1)
go func() {
result <- ui.Ask(message)
}()
for {
select {
case <-result:
return
case <-time.After(100 * time.Millisecond):
if _, ok := state[multistep.StateCancelled]; ok {
return
}
}
}
}
}

View File

@ -219,7 +219,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {

View File

@ -7,8 +7,8 @@ import (
)
type testUi struct {
askCalled bool
askQuery string
askCalled bool
askQuery string
errorCalled bool
errorMessage string
messageCalled bool

View File

@ -110,7 +110,7 @@ func (rw *ReaderWriterUi) Ask(query string) string {
var line string
if _, err := fmt.Fscanln(rw.Reader, &line); err != nil {
panic(err)
log.Printf("ui: scan err: %s", err)
}
return line