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!
This commit is contained in:
parent
cfc7715495
commit
5decc186a6
|
@ -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) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
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 {
|
||||||
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 {
|
||||||
paths := Paths{
|
path, err := workstationVMwareRoot()
|
||||||
[]string{os.Getenv("VMWARE_HOME"), ""},
|
if err != nil {
|
||||||
[]string{path, ""},
|
log.Printf("Error finding VMware root: %s", err)
|
||||||
[]string{os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation"},
|
|
||||||
[]string{os.Getenv("ProgramFiles"), "/VMware/VMware Workstation"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return findFile(file, paths)
|
paths := make([]string, 0, 5)
|
||||||
}
|
if os.Getenv("VMWARE_HOME") != "" {
|
||||||
|
paths = append(paths, os.Getenv("VMWARE_HOME"))
|
||||||
func findDataFile(file string) string {
|
}
|
||||||
path, _ := workstationVmnetDhcpLeasesPathFromRegistry()
|
|
||||||
|
|
||||||
if path != "" {
|
if path != "" {
|
||||||
path = filepath.Dir(path)
|
paths = append(paths, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
paths := Paths{
|
if os.Getenv("ProgramFiles(x86)") != "" {
|
||||||
[]string{os.Getenv("VMWARE_DATA"), ""},
|
paths = append(paths,
|
||||||
[]string{path, ""},
|
filepath.Join(os.Getenv("ProgramFiles(x86)"), "/VMware/VMware Workstation"))
|
||||||
[]string{os.Getenv("ProgramData"), "/VMWare"},
|
|
||||||
[]string{os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMWare"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return findFile(file, paths)
|
if os.Getenv("ProgramFiles") != "" {
|
||||||
|
paths = append(paths,
|
||||||
|
filepath.Join(os.Getenv("ProgramFiles"), "/VMware/VMware Workstation"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
func workstationVmnetnatConfPath() string {
|
// workstationDataFilePaths returns a list of paths that are eligible
|
||||||
const VMNETNAT_CONF = "vmnetnat.conf"
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
return findDataFile(VMNETNAT_CONF)
|
if leasesPath != "" {
|
||||||
|
leasesPath = filepath.Dir(leasesPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
paths := make([]string, 0, 5)
|
||||||
|
if os.Getenv("VMWARE_DATA") != "" {
|
||||||
|
paths = append(paths, os.Getenv("VMWARE_DATA"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if path != "" {
|
||||||
|
paths = append(paths, leasesPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("ProgramData") != "" {
|
||||||
|
paths = append(paths,
|
||||||
|
filepath.Join(os.Getenv("ProgramData"), "/VMware"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("ALLUSERSPROFILE") != "" {
|
||||||
|
paths = append(paths,
|
||||||
|
filepath.Join(os.Getenv("ALLUSERSPROFILE"), "/Application Data/VMware"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return paths
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue