-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
79 lines (65 loc) · 1.95 KB
/
util.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"github.com/mitchellh/packer/packer"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func FindOvfTool() (ovftool string, err error) {
// Accumulate any errors
errs := new(packer.MultiError)
// use ovftool in PATH, so use can decide which one to use
ovftool = "ovftool"
if _, err := exec.LookPath(ovftool); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("ovftool not found in path: %s", err))
files := make([]string, 0, 6)
// search ovftool at some specific places
files = append(files, "/Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool/ovftool")
if os.Getenv("ProgramFiles(x86)") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/Client Integration Plug-in 5.5/ovftool/ovftool.exe"))
}
if os.Getenv("ProgramFiles") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/Client Integration Plug-in 5.5/ovftool/ovftool.exe"))
}
if os.Getenv("ProgramFiles(x86)") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation/ovftool/ovftool.exe"))
}
if os.Getenv("ProgramFiles") != "" {
files = append(files,
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Workstation/ovftool/ovftool.exe"))
}
file := findFile(files)
if file == "" {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("ovftool not found: %s", err))
} else {
ovftool = file
}
}
return
}
func findFile(files []string) string {
for _, file := range files {
file = normalizePath(file)
log.Printf("Searching for file '%s'", file)
if _, err := os.Stat(file); err == nil {
log.Printf("Found file '%s'", file)
return file
}
}
log.Printf("File not found")
return ""
}
func normalizePath(path string) string {
path = strings.Replace(path, "\\", "/", -1)
path = strings.Replace(path, "//", "/", -1)
path = strings.TrimRight(path, "/")
return path
}