packer-cn/builder/hyperv/common/step_create_tempdir.go

52 lines
1.0 KiB
Go

package common
import (
"fmt"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"io/ioutil"
"os"
)
type StepCreateTempDir struct {
dirPath string
}
func (s *StepCreateTempDir) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating temporary directory...")
tempDir := os.TempDir()
packerTempDir, err := ioutil.TempDir(tempDir, "packerhv")
if err != nil {
err := fmt.Errorf("Error creating temporary directory: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
s.dirPath = packerTempDir
state.Put("packerTempDir", packerTempDir)
// ui.Say("packerTempDir = '" + packerTempDir + "'")
return multistep.ActionContinue
}
func (s *StepCreateTempDir) Cleanup(state multistep.StateBag) {
if s.dirPath == "" {
return
}
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting temporary directory...")
err := os.RemoveAll(s.dirPath)
if err != nil {
ui.Error(fmt.Sprintf("Error deleting temporary directory: %s", err))
}
}