gofmt fixes, improved/added log messages, fixes #221/#222

This commit is contained in:
Ross Smith II 2013-07-27 14:59:23 -07:00
parent 1cc36f11bc
commit 2e4585bc9c
3 changed files with 66 additions and 54 deletions

View File

@ -11,8 +11,8 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"
"unsafe"
"syscall"
)
// Workstation9Driver is a driver that can run VMware Workstation 9
@ -104,16 +104,16 @@ func (d *Workstation9Driver) Verify() error {
}
// Check to see if it APPEARS to be licensed.
/*
matches, err := filepath.Glob("/etc/vmware/license-*")
if err != nil {
return fmt.Errorf("Error looking for VMware license: %s", err)
}
/*
matches, err := filepath.Glob("/etc/vmware/license-*")
if err != nil {
return fmt.Errorf("Error looking for VMware license: %s", err)
}
if len(matches) == 0 {
return errors.New("Workstation does not appear to be licensed. Please license it.")
}
*/
if len(matches) == 0 {
return errors.New("Workstation does not appear to be licensed. Please license it.")
}
*/
return nil
}
@ -126,6 +126,7 @@ func (d *Workstation9Driver) findApp() error {
}
path += "vmware.exe"
}
path = strings.Replace(path, "\\", "/", -1)
log.Printf("Using '%s' for vmware path", path)
d.AppPath = path
@ -141,6 +142,7 @@ func (d *Workstation9Driver) findVdiskManager() error {
}
path += "vmware-vdiskmanager.exe"
}
path = strings.Replace(path, "\\", "/", -1)
log.Printf("Using '%s' for vmware-vdiskmanager path", path)
d.VdiskManagerPath = path
return nil
@ -155,6 +157,7 @@ func (d *Workstation9Driver) findVmrun() error {
}
path += "vmrun.exe"
}
path = strings.Replace(path, "\\", "/", -1)
log.Printf("Using '%s' for vmrun path", path)
d.VmrunPath = path
return nil
@ -203,41 +206,41 @@ func (d *Workstation9Driver) runAndLog(cmd *exec.Cmd) (string, string, error) {
// see http://blog.natefinch.com/2012/11/go-win-stuff.html
func readRegString(hive syscall.Handle, subKeyPath, valueName string) (value string, err error) {
var h syscall.Handle
err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h)
if err != nil {
return
}
defer syscall.RegCloseKey(h)
var h syscall.Handle
err = syscall.RegOpenKeyEx(hive, syscall.StringToUTF16Ptr(subKeyPath), 0, syscall.KEY_READ, &h)
if err != nil {
return
}
defer syscall.RegCloseKey(h)
var typ uint32
var bufSize uint32
var typ uint32
var bufSize uint32
err = syscall.RegQueryValueEx(
h,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
nil,
&bufSize)
if err != nil {
return
}
err = syscall.RegQueryValueEx(
h,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
nil,
&bufSize)
if err != nil {
return
}
data := make([]uint16, bufSize/2+1)
data := make([]uint16, bufSize/2+1)
err = syscall.RegQueryValueEx(
h,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
(*byte)(unsafe.Pointer(&data[0])),
&bufSize)
if err != nil {
return
}
err = syscall.RegQueryValueEx(
h,
syscall.StringToUTF16Ptr(valueName),
nil,
&typ,
(*byte)(unsafe.Pointer(&data[0])),
&bufSize)
if err != nil {
return
}
return syscall.UTF16ToString(data), nil
return syscall.UTF16ToString(data), nil
}
func getVmwarePath() (s string, e error) {

View File

@ -51,7 +51,7 @@ func (f *DHCPLeaseGuestLookup) GuestIP() (string, error) {
for _, line := range strings.Split(string(dhcpBytes), "\n") {
// Need to trim off CR character when running in windows
line = strings.TrimRight(line, "\r");
line = strings.TrimRight(line, "\r")
matches := ipLineRe.FindStringSubmatch(line)
if matches != nil {

View File

@ -5,12 +5,12 @@ package vmware
import (
"errors"
"regexp"
"io/ioutil"
"log"
"strings"
"net"
"os"
"io/ioutil"
"regexp"
"strings"
)
// Interface to help find the host IP that is available from within
@ -38,12 +38,13 @@ func (f *IfconfigIPFinder) HostIP() (string, error) {
log.Printf("Searching for MAC %s", vmwareMac)
re := regexp.MustCompile("(?i)^" + vmwareMac)
var rv string
ip := ""
for _, ifi := range ift {
log.Printf("Found interface %s", ifi.HardwareAddr.String())
mac := ifi.HardwareAddr.String()
log.Printf("Found MAC %s", mac)
matches := re.FindStringSubmatch(ifi.HardwareAddr.String())
matches := re.FindStringSubmatch(mac)
if matches == nil {
continue
@ -51,23 +52,25 @@ func (f *IfconfigIPFinder) HostIP() (string, error) {
addrs, err := ifi.Addrs()
if err != nil {
log.Printf("No IP addresses found for %s", ifi.HardwareAddr.String())
log.Printf("No IP addresses found for MAC %s", mac)
continue
}
for _, address := range addrs {
rv = address.String()
log.Printf("Found VMWare IP address %s", address.String())
ip = address.String()
log.Printf("Found IP address %s for MAC %s", ip, mac)
}
// continue looping as VMNet8 comes after VMNet1 (at least on my system)
}
if rv > "" {
return rv, nil
if ip == "" {
return "", errors.New("No MACs found matching " + vmwareMac)
}
return "", errors.New("No VMWare MAC addresses found")
log.Printf("Returning IP address %s", ip)
return ip, nil
}
func getVMWareMAC() (string, error) {
@ -75,12 +78,15 @@ func getVMWareMAC() (string, error) {
const defaultMacRe = "00:50:56"
programData := os.Getenv("ProgramData")
programData = strings.Replace(programData, "\\", "/", -1)
vmnetnat := programData + "/VMware/vmnetnat.conf"
if _, err := os.Stat(vmnetnat); os.IsNotExist(err) {
log.Printf("File not found: '%s' (found '%s' in %%ProgramData%%)", vmnetnat, programData)
return defaultMacRe, err
}
log.Printf("Searching for key hostMAC in '%s'", vmnetnat)
fh, err := os.Open(vmnetnat)
if err != nil {
return defaultMacRe, err
@ -96,14 +102,17 @@ func getVMWareMAC() (string, error) {
for _, line := range strings.Split(string(bytes), "\n") {
// Need to trim off CR character when running in windows
line = strings.TrimRight(line, "\r");
line = strings.TrimRight(line, "\r")
matches := hostMacRe.FindStringSubmatch(line)
if matches != nil {
log.Printf("Found MAC '%s' in '%s'", matches[1], vmnetnat)
return matches[1], nil
}
}
log.Printf("Did not find key hostMAC in '%s', using %s instead", vmnetnat, defaultMacRe)
return defaultMacRe, nil
}