github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/parallels/common/artifact.go (about)

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