2020-11-11 18:04:28 -05:00
|
|
|
package commonsteps
|
2013-06-14 18:24:53 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-15 21:24:38 -04:00
|
|
|
"log"
|
2013-06-14 18:47:06 -04:00
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-06-14 18:24:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// MultistepDebugFn will return a proper multistep.DebugPauseFn to
|
|
|
|
// use for debugging if you're using multistep in your builder.
|
2020-11-19 14:54:31 -05:00
|
|
|
func MultistepDebugFn(ui packersdk.Ui) multistep.DebugPauseFn {
|
2013-08-31 15:17:59 -04:00
|
|
|
return func(loc multistep.DebugLocation, name string, state multistep.StateBag) {
|
2013-06-14 18:24:53 -04:00
|
|
|
var locationString string
|
|
|
|
switch loc {
|
|
|
|
case multistep.DebugLocationAfterRun:
|
|
|
|
locationString = "after run of"
|
|
|
|
case multistep.DebugLocationBeforeCleanup:
|
|
|
|
locationString = "before cleanup of"
|
|
|
|
default:
|
|
|
|
locationString = "at"
|
|
|
|
}
|
|
|
|
|
|
|
|
message := fmt.Sprintf(
|
2014-03-13 00:40:27 -04:00
|
|
|
"Pausing %s step '%s'. Press enter to continue.",
|
2013-06-14 18:24:53 -04:00
|
|
|
locationString, name)
|
|
|
|
|
2013-06-14 18:47:06 -04:00
|
|
|
result := make(chan string, 1)
|
|
|
|
go func() {
|
2013-06-15 21:24:38 -04:00
|
|
|
line, err := ui.Ask(message)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error asking for input: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result <- line
|
2013-06-14 18:47:06 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-result:
|
|
|
|
return
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2013-08-31 15:17:59 -04:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-06-14 18:47:06 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 18:24:53 -04:00
|
|
|
}
|
|
|
|
}
|