github.phpd.cn/hashicorp/packer@v1.3.2/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/hashicorp/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 if err != nil { 32 if os.IsNotExist(err) { 33 // It's possible that the path in question existed prior to visit being invoked, but has 34 // since been deleted. This happens, for example, if you have the VM console open while 35 // building. Opening the console creates a <vm name>.app directory which is gone by the time 36 // visit is invoked, resulting in err being a "file not found" error. In this case, skip the 37 // entire path and continue to the next one. 38 return filepath.SkipDir 39 } 40 return err 41 } 42 for _, unnecessaryFile := range unnecessaryFiles { 43 if unnecessary, _ := regexp.MatchString(unnecessaryFile, path); unnecessary { 44 return os.RemoveAll(path) 45 } 46 } 47 48 if !info.IsDir() { 49 files = append(files, path) 50 } 51 52 return nil 53 } 54 55 if err := filepath.Walk(dir, visit); err != nil { 56 return nil, err 57 } 58 59 return &artifact{ 60 dir: dir, 61 f: files, 62 }, nil 63 } 64 65 func (*artifact) BuilderId() string { 66 return BuilderId 67 } 68 69 func (a *artifact) Files() []string { 70 return a.f 71 } 72 73 func (*artifact) Id() string { 74 return "VM" 75 } 76 77 func (a *artifact) String() string { 78 return fmt.Sprintf("VM files in directory: %s", a.dir) 79 } 80 81 func (a *artifact) State(name string) interface{} { 82 return nil 83 } 84 85 func (a *artifact) Destroy() error { 86 return os.RemoveAll(a.dir) 87 }