Commit 060ae56b authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: nitpick some styles

/cc @rasa - I changed up quite a bit here. I tried to reduce function
count if possible, renamed some functions, etc. Overall the
functionality was all spot on, but I felt the functions were too
specialized. Thanks!
parent 1e18249d
...@@ -18,44 +18,49 @@ func workstationCheckLicense() error { ...@@ -18,44 +18,49 @@ func workstationCheckLicense() error {
} }
func workstationFindVdiskManager() (string, error) { func workstationFindVdiskManager() (string, error) {
path, _ := exec.LookPath("vmware-vdiskmanager.exe") path, err := exec.LookPath("vmware-vdiskmanager.exe")
if fileExists(path) { if err == nil {
return path, nil return path, nil
} }
return findProgramFile("vmware-vdiskmanager.exe"), nil return findFile("vmware-vdiskmanager.exe", workstationProgramFilePaths()), nil
} }
func workstationFindVMware() (string, error) { func workstationFindVMware() (string, error) {
path, _ := exec.LookPath("vmware.exe") path, _ := exec.LookPath("vmware.exe")
if fileExists(path) { if err == nil {
return path, nil return path, nil
} }
return findProgramFile("vmware.exe"), nil return findFile("vmware.exe", workstationProgramFilePaths()), nil
} }
func workstationFindVmrun() (string, error) { func workstationFindVmrun() (string, error) {
path, _ := exec.LookPath("vmrun.exe") path, _ := exec.LookPath("vmrun.exe")
if fileExists(path) { if err == nil {
return path, nil return path, nil
} }
return findProgramFile("vmrun.exe"), nil return findFile("vmrun.exe", workstationProgramFilePaths()), nil
} }
func workstationToolsIsoPath(flavor string) string { func workstationToolsIsoPath(flavor string) string {
return findProgramFile(flavor + ".iso") return findFile(flavor+".iso", workstationProgramFilePaths()), nil
} }
func workstationDhcpLeasesPath(device string) string { func workstationDhcpLeasesPath(device string) string {
path, _ := workstationVmnetDhcpLeasesPathFromRegistry() path, err := workstationDhcpLeasesPathRegistry()
if err != nil {
if fileExists(path) { log.Printf("Error finding leases in registry: %s", err)
} else if _, err := os.Stat(path); err == nil {
return path return path
} }
return findDataFile("vmnetdhcp.leases") return findFile("vmnetdhcp.leases", workstationDataFilePaths()), nil
}
func workstationVmnetnatConfPath() string {
return findFile("vmnetnat.conf", workstationDataFilePaths()), nil
} }
// See http://blog.natefinch.com/2012/11/go-win-stuff.html // See http://blog.natefinch.com/2012/11/go-win-stuff.html
...@@ -111,7 +116,7 @@ func workstationVMwareRoot() (s string, err error) { ...@@ -111,7 +116,7 @@ func workstationVMwareRoot() (s string, err error) {
} }
// This reads the VMware DHCP leases path from the Windows registry. // This reads the VMware DHCP leases path from the Windows registry.
func workstationVmnetDhcpLeasesPathFromRegistry() (s string, err error) { func workstationDhcpLeasesPathRegistry() (s string, err error) {
key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters" key := "SYSTEM\\CurrentControlSet\\services\\VMnetDHCP\\Parameters"
subkey := "LeaseFile" subkey := "LeaseFile"
s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey) s, err = readRegString(syscall.HKEY_LOCAL_MACHINE, key, subkey)
...@@ -123,21 +128,6 @@ func workstationVmnetDhcpLeasesPathFromRegistry() (s string, err error) { ...@@ -123,21 +128,6 @@ func workstationVmnetDhcpLeasesPathFromRegistry() (s string, err error) {
return normalizePath(s), nil return normalizePath(s), nil
} }
func fileExists(file string) bool {
if file == "" {
return false
}
if _, err := os.Stat(file); err != nil {
if os.IsNotExist(err) {
return false
}
log.Println(err.Error())
}
return true
}
func normalizePath(path string) string { func normalizePath(path string) string {
path = strings.Replace(path, "\\", "/", -1) path = strings.Replace(path, "\\", "/", -1)
path = strings.Replace(path, "//", "/", -1) path = strings.Replace(path, "//", "/", -1)
...@@ -145,63 +135,82 @@ func normalizePath(path string) string { ...@@ -145,63 +135,82 @@ func normalizePath(path string) string {
return path return path
} }
type Paths [][]string func findFile(file string, paths []string) string {
for _, path := range paths {
func findFile(file string, paths Paths) string { path = filepath.Join(path, file)
for _, a := range paths {
if a[0] == "" {
continue
}
path := filepath.Join(a[0], a[1], file)
path = normalizePath(path) path = normalizePath(path)
log.Printf("Searching for file '%s'", path) log.Printf("Searching for file '%s'", path)
if fileExists(path) { if _, err := os.Stat(path); err != nil {
log.Printf("Found file '%s'", path) log.Printf("Found file '%s'", path)
return path return path
} }
} }
log.Printf("File not found: '%s'", file) log.Printf("File not found: '%s'", file)
return "" return ""
} }
func findProgramFile(file string) string { // workstationProgramFilesPaths returns a list of paths that are eligible
path, _ := workstationVMwareRoot() // to contain program files we may want just as vmware.exe.
func workstationProgramFilePaths() []string {
path, err := workstationVMwareRoot()
if err != nil {
log.Printf("Error finding VMware root: %s", err)
}
paths := Paths{ paths := make([]string, 0, 5)
[]string{os.Getenv("VMWARE_HOME"), ""}, if os.Getenv("VMWARE_HOME") != "" {
[]string{path, ""}, paths = append(paths, os.Getenv("VMWARE_HOME"))
[]string{os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation"},
[]string{os.Getenv("ProgramFiles"), "/VMware/VMware Workstation"},
} }
return findFile(file, paths) if path != "" {
paths = append(paths, path)
}
if os.Getenv("ProgramFiles(x86)") != "" {
paths = append(paths,
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation"))
}
if os.Getenv("ProgramFiles") != "" {
paths = append(paths,
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Workstation"))
}
return paths
} }
func findDataFile(file string) string { // workstationDataFilePaths returns a list of paths that are eligible
path, _ := workstationVmnetDhcpLeasesPathFromRegistry() // to contain data files we may want such as vmnet NAT configuration files.
func workstationDataFilePaths() []string {
leasesPath, err := workstationVmnetDhcpLeasesPathFromRegistry()
if err != nil {
log.Printf("Error getting DHCP leases path: %s", err)
}
if leasesPath != "" {
leasesPath = filepath.Dir(leasesPath)
}
if path != "" { paths := make([]string, 0, 5)
path = filepath.Dir(path) if os.Getenv("VMWARE_DATA") != "" {
paths = append(paths, os.Getenv("VMWARE_DATA"))
} }
paths := Paths{ if path != "" {
[]string{os.Getenv("VMWARE_DATA"), ""}, paths = append(paths, leasesPath)
[]string{path, ""},
[]string{os.Getenv("ProgramData"), "/VMWare"},
[]string{os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMWare"},
} }
return findFile(file, paths) if os.Getenv("ProgramData") != "" {
} paths = append(paths,
filepath.Join(os.Getenv("ProgramData"), "/VMware"))
}
func workstationVmnetnatConfPath() string { if os.Getenv("ALLUSERSPROFILE") != "" {
const VMNETNAT_CONF = "vmnetnat.conf" paths = append(paths,
filepath.Join(os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMware"))
}
return findDataFile(VMNETNAT_CONF) return paths
} }
...@@ -16,18 +16,15 @@ import ( ...@@ -16,18 +16,15 @@ import (
type VMnetNatConfIPFinder struct{} type VMnetNatConfIPFinder struct{}
func (*VMnetNatConfIPFinder) HostIP() (string, error) { func (*VMnetNatConfIPFinder) HostIP() (string, error) {
const VMNETNAT_CONF = "vmnetnat.conf"
driver := &Workstation9Driver{} driver := &Workstation9Driver{}
vmnetnat := driver.VmnetnatConfPath() vmnetnat := driver.VmnetnatConfPath()
if vmnetnat == "" { if vmnetnat == "" {
return "", fmt.Errorf("Could not find %s", VMNETNAT_CONF) return "", errors.New("Could not find NAT vmnet conf file")
} }
if _, err := os.Stat(vmnetnat); err != nil { if _, err := os.Stat(vmnetnat); err != nil {
return "", fmt.Errorf("Error with %s: %s", VMNETNAT_CONF, err) return "", fmt.Errorf("Could not find NAT vmnet conf file: %s", vmnetnat)
} }
f, err := os.Open(vmnetnat) f, err := os.Open(vmnetnat)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment