github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/hyperv/common/step_create_tempdir.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"io/ioutil"
     8  	"os"
     9  )
    10  
    11  type StepCreateTempDir struct {
    12  	dirPath string
    13  }
    14  
    15  func (s *StepCreateTempDir) Run(state multistep.StateBag) multistep.StepAction {
    16  	ui := state.Get("ui").(packer.Ui)
    17  
    18  	ui.Say("Creating temporary directory...")
    19  
    20  	tempDir := os.TempDir()
    21  	packerTempDir, err := ioutil.TempDir(tempDir, "packerhv")
    22  	if err != nil {
    23  		err := fmt.Errorf("Error creating temporary directory: %s", err)
    24  		state.Put("error", err)
    25  		ui.Error(err.Error())
    26  		return multistep.ActionHalt
    27  	}
    28  
    29  	s.dirPath = packerTempDir
    30  	state.Put("packerTempDir", packerTempDir)
    31  
    32  	//	ui.Say("packerTempDir = '" + packerTempDir + "'")
    33  
    34  	return multistep.ActionContinue
    35  }
    36  
    37  func (s *StepCreateTempDir) Cleanup(state multistep.StateBag) {
    38  	if s.dirPath == "" {
    39  		return
    40  	}
    41  
    42  	ui := state.Get("ui").(packer.Ui)
    43  
    44  	ui.Say("Deleting temporary directory...")
    45  
    46  	err := os.RemoveAll(s.dirPath)
    47  
    48  	if err != nil {
    49  		ui.Error(fmt.Sprintf("Error deleting temporary directory: %s", err))
    50  	}
    51  }