forked from hashicorp/packer
/
parallels.go
58 lines (48 loc) · 1.53 KB
/
parallels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package vagrant
import (
"fmt"
"path/filepath"
"regexp"
"github.com/hashicorp/packer/packer"
)
// These are the extensions of files and directories that are unnecessary for the function
// of a Parallels virtual machine.
var UnnecessaryFilesPatterns = []string{"\\.log$", "\\.backup$", "\\.Backup$", "\\.app/", "/Windows Disks/"}
type ParallelsProvider struct{}
func (p *ParallelsProvider) KeepInputArtifact() bool {
return false
}
func (p *ParallelsProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
// Create the metadata
metadata = map[string]interface{}{"provider": "parallels"}
// Copy all of the original contents into the temporary directory
for _, path := range artifact.Files() {
// If the file isn't critical to the function of the
// virtual machine, we get rid of it.
unnecessary := false
for _, unnecessaryPat := range UnnecessaryFilesPatterns {
if matched, _ := regexp.MatchString(unnecessaryPat, path); matched {
unnecessary = true
break
}
}
if unnecessary {
continue
}
tmpPath := filepath.ToSlash(path)
pathRe := regexp.MustCompile(`^(.+?)([^/]+\.pvm/.+?)$`)
matches := pathRe.FindStringSubmatch(tmpPath)
var pvmPath string
if matches != nil {
pvmPath = filepath.FromSlash(matches[2])
} else {
continue // Just copy a pvm
}
dstPath := filepath.Join(dir, pvmPath)
ui.Message(fmt.Sprintf("Copying: %s", path))
if err = CopyContents(dstPath, path); err != nil {
return
}
}
return
}