github.com/alouche/packer@v0.3.7/builder/vmware/step_clean_files.go (about)

     1  package vmware
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"os"
     8  	"path/filepath"
     9  )
    10  
    11  // These are the extensions of files that are important for the function
    12  // of a VMware virtual machine. Any other file is discarded as part of the
    13  // build.
    14  var KeepFileExtensions = []string{".nvram", ".vmdk", ".vmsd", ".vmx", ".vmxf"}
    15  
    16  // This step removes unnecessary files from the final result.
    17  //
    18  // Uses:
    19  //   config *config
    20  //   ui     packer.Ui
    21  //
    22  // Produces:
    23  //   <nothing>
    24  type stepCleanFiles struct{}
    25  
    26  func (stepCleanFiles) Run(state multistep.StateBag) multistep.StepAction {
    27  	config := state.Get("config").(*config)
    28  	ui := state.Get("ui").(packer.Ui)
    29  
    30  	ui.Say("Deleting unnecessary VMware files...")
    31  	visit := func(path string, info os.FileInfo, err error) error {
    32  		if err != nil {
    33  			return err
    34  		}
    35  
    36  		if !info.IsDir() {
    37  			// If the file isn't critical to the function of the
    38  			// virtual machine, we get rid of it.
    39  			keep := false
    40  			ext := filepath.Ext(path)
    41  			for _, goodExt := range KeepFileExtensions {
    42  				if goodExt == ext {
    43  					keep = true
    44  					break
    45  				}
    46  			}
    47  
    48  			if !keep {
    49  				ui.Message(fmt.Sprintf("Deleting: %s", path))
    50  				return os.Remove(path)
    51  			}
    52  		}
    53  
    54  		return nil
    55  	}
    56  
    57  	if err := filepath.Walk(config.OutputDir, visit); err != nil {
    58  		state.Put("error", err)
    59  		return multistep.ActionHalt
    60  	}
    61  
    62  	return multistep.ActionContinue
    63  }
    64  
    65  func (stepCleanFiles) Cleanup(multistep.StateBag) {}