github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/common/multistep_debug.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"log"
     8  	"time"
     9  )
    10  
    11  // MultistepDebugFn will return a proper multistep.DebugPauseFn to
    12  // use for debugging if you're using multistep in your builder.
    13  func MultistepDebugFn(ui packer.Ui) multistep.DebugPauseFn {
    14  	return func(loc multistep.DebugLocation, name string, state map[string]interface{}) {
    15  		var locationString string
    16  		switch loc {
    17  		case multistep.DebugLocationAfterRun:
    18  			locationString = "after run of"
    19  		case multistep.DebugLocationBeforeCleanup:
    20  			locationString = "before cleanup of"
    21  		default:
    22  			locationString = "at"
    23  		}
    24  
    25  		message := fmt.Sprintf(
    26  			"Pausing %s step '%s'. Press any key to continue.",
    27  			locationString, name)
    28  
    29  		result := make(chan string, 1)
    30  		go func() {
    31  			line, err := ui.Ask(message)
    32  			if err != nil {
    33  				log.Printf("Error asking for input: %s", err)
    34  			}
    35  
    36  			result <- line
    37  		}()
    38  
    39  		for {
    40  			select {
    41  			case <-result:
    42  				return
    43  			case <-time.After(100 * time.Millisecond):
    44  				if _, ok := state[multistep.StateCancelled]; ok {
    45  					return
    46  				}
    47  			}
    48  		}
    49  	}
    50  }