From 442c5e3e3b60bb477e6e36b59dc8dae41c0d76d2 Mon Sep 17 00:00:00 2001 From: "Jason A. Beranek" Date: Fri, 12 Jul 2013 21:23:00 -0500 Subject: [PATCH] Remove output directory in a step when "-force" flag step [GH-178] --- builder/virtualbox/builder.go | 5 +---- builder/virtualbox/step_prepare_output_dir.go | 6 ++++++ builder/vmware/builder.go | 5 +---- builder/vmware/step_prepare_output_dir.go | 6 ++++++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 5b5dbfd92..581b01fb1 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -220,10 +220,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { } if _, err := os.Stat(b.config.OutputDir); err == nil { - if b.config.PackerForce { - log.Printf("Build forced, removing existing output directory: %s", string(b.config.OutputDir)) - os.RemoveAll(b.config.OutputDir) - } else { + if !b.config.PackerForce { errs = append(errs, errors.New("Output directory already exists. It must not exist.")) } } diff --git a/builder/virtualbox/step_prepare_output_dir.go b/builder/virtualbox/step_prepare_output_dir.go index 48e1eabc7..102f71035 100644 --- a/builder/virtualbox/step_prepare_output_dir.go +++ b/builder/virtualbox/step_prepare_output_dir.go @@ -10,6 +10,12 @@ type stepPrepareOutputDir struct{} func (stepPrepareOutputDir) Run(state map[string]interface{}) multistep.StepAction { config := state["config"].(*config) + ui := state["ui"].(packer.Ui) + + if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce { + ui.Say("Deleting previous output directory...") + os.RemoveAll(config.OutputDir) + } if err := os.MkdirAll(config.OutputDir, 0755); err != nil { state["error"] = err diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 3bbf0b20f..60ed66f40 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -176,10 +176,7 @@ func (b *Builder) Prepare(raws ...interface{}) error { } if _, err := os.Stat(b.config.OutputDir); err == nil { - if b.config.PackerForce { - log.Printf("Build forced, removing existing output directory: %s", string(b.config.OutputDir)) - os.RemoveAll(b.config.OutputDir) - } else { + if !b.config.PackerForce { errs = append(errs, errors.New("Output directory already exists. It must not exist.")) } } diff --git a/builder/vmware/step_prepare_output_dir.go b/builder/vmware/step_prepare_output_dir.go index fd194c91c..8e3a41350 100644 --- a/builder/vmware/step_prepare_output_dir.go +++ b/builder/vmware/step_prepare_output_dir.go @@ -10,6 +10,12 @@ type stepPrepareOutputDir struct{} func (stepPrepareOutputDir) Run(state map[string]interface{}) multistep.StepAction { config := state["config"].(*config) + ui := state["ui"].(packer.Ui) + + if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce { + ui.Say("Deleting previous output directory...") + os.RemoveAll(config.OutputDir) + } if err := os.MkdirAll(config.OutputDir, 0755); err != nil { state["error"] = err