From 597b7ad8c53af772c775da453db1d56120faf2c2 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Wed, 8 Mar 2017 23:43:38 -0800 Subject: [PATCH] builder/hyper-v: validate output dir in step, not in config essentially same work as #2233 --- builder/hyperv/common/output_config.go | 12 ++---------- builder/hyperv/common/output_config_test.go | 7 ++++--- builder/hyperv/common/step_output_dir.go | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/builder/hyperv/common/output_config.go b/builder/hyperv/common/output_config.go index ee589d49d..7b5ddcd45 100644 --- a/builder/hyperv/common/output_config.go +++ b/builder/hyperv/common/output_config.go @@ -2,9 +2,9 @@ package common import ( "fmt" + "github.com/mitchellh/packer/common" "github.com/mitchellh/packer/template/interpolate" - "os" ) type OutputConfig struct { @@ -16,13 +16,5 @@ func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName) } - var errs []error - if !pc.PackerForce { - if _, err := os.Stat(c.OutputDir); err == nil { - errs = append(errs, fmt.Errorf( - "Output directory '%s' already exists. It must not exist.", c.OutputDir)) - } - } - - return errs + return nil } diff --git a/builder/hyperv/common/output_config_test.go b/builder/hyperv/common/output_config_test.go index ebd91eab1..3da8bda58 100644 --- a/builder/hyperv/common/output_config_test.go +++ b/builder/hyperv/common/output_config_test.go @@ -1,10 +1,11 @@ package common import ( - "github.com/mitchellh/packer/common" "io/ioutil" "os" "testing" + + "github.com/mitchellh/packer/common" ) func TestOutputConfigPrepare(t *testing.T) { @@ -39,7 +40,7 @@ func TestOutputConfigPrepare_exists(t *testing.T) { PackerForce: false, } errs := c.Prepare(testConfigTemplate(t), pc) - if len(errs) == 0 { - t.Fatal("should have errors") + if len(errs) != 0 { + t.Fatal("should not have errors") } } diff --git a/builder/hyperv/common/step_output_dir.go b/builder/hyperv/common/step_output_dir.go index 209bbabe2..1443e5791 100644 --- a/builder/hyperv/common/step_output_dir.go +++ b/builder/hyperv/common/step_output_dir.go @@ -17,16 +17,30 @@ import ( type StepOutputDir struct { Force bool Path string + + cleanup bool } func (s *StepOutputDir) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) - if _, err := os.Stat(s.Path); err == nil && s.Force { + if _, err := os.Stat(s.Path); err == nil { + if !s.Force { + err := fmt.Errorf( + "Output directory exists: %s\n\n"+ + "Use the force flag to delete it prior to building.", + s.Path) + state.Put("error", err) + return multistep.ActionHalt + } + ui.Say("Deleting previous output directory...") os.RemoveAll(s.Path) } + // Enable cleanup + s.cleanup = true + // Create the directory if err := os.MkdirAll(s.Path, 0755); err != nil { state.Put("error", err) @@ -47,6 +61,10 @@ func (s *StepOutputDir) Run(state multistep.StateBag) multistep.StepAction { } func (s *StepOutputDir) Cleanup(state multistep.StateBag) { + if !s.cleanup { + return + } + _, cancelled := state.GetOk(multistep.StateCancelled) _, halted := state.GetOk(multistep.StateHalted)