|
|
|
|
@ -4,7 +4,6 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -16,7 +15,7 @@ var KeepFileExtensions = []string{".nvram", ".vmdk", ".vmsd", ".vmx", ".vmxf"}
|
|
|
|
|
// This step removes unnecessary files from the final result.
|
|
|
|
|
//
|
|
|
|
|
// Uses:
|
|
|
|
|
// config *config
|
|
|
|
|
// dir OutputDir
|
|
|
|
|
// ui packer.Ui
|
|
|
|
|
//
|
|
|
|
|
// Produces:
|
|
|
|
|
@ -24,39 +23,35 @@ var KeepFileExtensions = []string{".nvram", ".vmdk", ".vmsd", ".vmx", ".vmxf"}
|
|
|
|
|
type stepCleanFiles struct{}
|
|
|
|
|
|
|
|
|
|
func (stepCleanFiles) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
|
dir := state.Get("dir").(OutputDir)
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
|
|
ui.Say("Deleting unnecessary VMware files...")
|
|
|
|
|
visit := func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
files, err := dir.ListFiles()
|
|
|
|
|
if err != nil {
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
|
// If the file isn't critical to the function of the
|
|
|
|
|
// virtual machine, we get rid of it.
|
|
|
|
|
keep := false
|
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
|
for _, goodExt := range KeepFileExtensions {
|
|
|
|
|
if goodExt == ext {
|
|
|
|
|
keep = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
for _, path := range files {
|
|
|
|
|
// If the file isn't critical to the function of the
|
|
|
|
|
// virtual machine, we get rid of it.
|
|
|
|
|
keep := false
|
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
|
for _, goodExt := range KeepFileExtensions {
|
|
|
|
|
if goodExt == ext {
|
|
|
|
|
keep = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !keep {
|
|
|
|
|
ui.Message(fmt.Sprintf("Deleting: %s", path))
|
|
|
|
|
return os.Remove(path)
|
|
|
|
|
if !keep {
|
|
|
|
|
ui.Message(fmt.Sprintf("Deleting: %s", path))
|
|
|
|
|
if err = dir.Remove(path); err != nil {
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := filepath.Walk(config.OutputDir, visit); err != nil {
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
|