github.com/rothwerx/packer@v0.9.0/builder/parallels/common/artifact.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/packer"
     6  	"os"
     7  	"path/filepath"
     8  	"regexp"
     9  )
    10  
    11  // This is the common builder ID to all of these artifacts.
    12  const BuilderId = "packer.parallels"
    13  
    14  // These are the extensions of files and directories that are unnecessary for the function
    15  // of a Parallels virtual machine.
    16  var unnecessaryFiles = []string{"\\.log$", "\\.backup$", "\\.Backup$", "\\.app"}
    17  
    18  // Artifact is the result of running the parallels builder, namely a set
    19  // of files associated with the resulting machine.
    20  type artifact struct {
    21  	dir string
    22  	f   []string
    23  }
    24  
    25  // NewArtifact returns a Parallels artifact containing the files
    26  // in the given directory.
    27  func NewArtifact(dir string) (packer.Artifact, error) {
    28  	files := make([]string, 0, 5)
    29  	visit := func(path string, info os.FileInfo, err error) error {
    30  		for _, unnecessaryFile := range unnecessaryFiles {
    31  			if unnecessary, _ := regexp.MatchString(unnecessaryFile, path); unnecessary {
    32  				return os.RemoveAll(path)
    33  			}
    34  		}
    35  
    36  		if !info.IsDir() {
    37  			files = append(files, path)
    38  		}
    39  
    40  		return err
    41  	}
    42  
    43  	if err := filepath.Walk(dir, visit); err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return &artifact{
    48  		dir: dir,
    49  		f:   files,
    50  	}, nil
    51  }
    52  
    53  func (*artifact) BuilderId() string {
    54  	return BuilderId
    55  }
    56  
    57  func (a *artifact) Files() []string {
    58  	return a.f
    59  }
    60  
    61  func (*artifact) Id() string {
    62  	return "VM"
    63  }
    64  
    65  func (a *artifact) String() string {
    66  	return fmt.Sprintf("VM files in directory: %s", a.dir)
    67  }
    68  
    69  func (a *artifact) State(name string) interface{} {
    70  	return nil
    71  }
    72  
    73  func (a *artifact) Destroy() error {
    74  	return os.RemoveAll(a.dir)
    75  }