From 21b6d2a43525362e0344cce3ea3f5393feddd9da Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 14 Jun 2013 15:47:06 -0700 Subject: [PATCH] builder/common: multistep debug fn gracefully exits during an interrupt --- builder/common/multistep_debug.go | 20 +++++++++++++++++--- builder/vmware/builder.go | 2 +- packer/rpc/ui_test.go | 4 ++-- packer/ui.go | 2 +- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/builder/common/multistep_debug.go b/builder/common/multistep_debug.go index 3b3a10af2..f0ac31029 100644 --- a/builder/common/multistep_debug.go +++ b/builder/common/multistep_debug.go @@ -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 + } + } + } } } diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 2503b6786..41f6365c4 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -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 { diff --git a/packer/rpc/ui_test.go b/packer/rpc/ui_test.go index 384b959f2..f267bf512 100644 --- a/packer/rpc/ui_test.go +++ b/packer/rpc/ui_test.go @@ -7,8 +7,8 @@ import ( ) type testUi struct { - askCalled bool - askQuery string + askCalled bool + askQuery string errorCalled bool errorMessage string messageCalled bool diff --git a/packer/ui.go b/packer/ui.go index c702b09f5..c191df9b7 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -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