From 35578d9ed112cb72d69bc706f8311cd29069387a Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 28 Mar 2017 18:47:10 -0700 Subject: [PATCH] remove unnecessary type conversions --- builder/amazon/common/access_config.go | 2 +- builder/amazon/common/state.go | 2 +- builder/azure/common/gluestrings.go | 2 +- builder/cloudstack/ssh.go | 2 +- builder/digitalocean/step_create_droplet.go | 5 +- builder/hyperv/common/driver_ps_4.go | 6 +- builder/hyperv/common/step_configure_ip.go | 7 +- builder/triton/access_config.go | 2 +- builder/virtualbox/common/ssh.go | 2 +- common/powershell/hyperv/hyperv.go | 74 ++++++++++----------- common/powershell/powershell.go | 2 +- communicator/ssh/password.go | 5 +- provisioner/ansible/scp.go | 2 +- 13 files changed, 58 insertions(+), 55 deletions(-) diff --git a/builder/amazon/common/access_config.go b/builder/amazon/common/access_config.go index 5e11fcd32..1fd66d091 100644 --- a/builder/amazon/common/access_config.go +++ b/builder/amazon/common/access_config.go @@ -115,5 +115,5 @@ func GetInstanceMetaData(path string) (contents []byte, err error) { if err != nil { return } - return []byte(body), err + return body, err } diff --git a/builder/amazon/common/state.go b/builder/amazon/common/state.go index e529f0c5f..42beb0e00 100644 --- a/builder/amazon/common/state.go +++ b/builder/amazon/common/state.go @@ -163,7 +163,7 @@ func WaitForState(conf *StateChangeConf) (i interface{}, err error) { log.Printf("Waiting for state to become: %s", conf.Target) sleepSeconds := SleepSeconds() - maxTicks := int(TimeoutSeconds()/sleepSeconds) + 1 + maxTicks := TimeoutSeconds()/sleepSeconds + 1 notfoundTick := 0 for { diff --git a/builder/azure/common/gluestrings.go b/builder/azure/common/gluestrings.go index 0988b98a9..b59b15fcf 100644 --- a/builder/azure/common/gluestrings.go +++ b/builder/azure/common/gluestrings.go @@ -18,5 +18,5 @@ func GlueStrings(a, b string) string { shift++ } - return string(a[:shift]) + b + return a[:shift] + b } diff --git a/builder/cloudstack/ssh.go b/builder/cloudstack/ssh.go index 1deabc9f6..b980102f4 100644 --- a/builder/cloudstack/ssh.go +++ b/builder/cloudstack/ssh.go @@ -44,7 +44,7 @@ func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) { return nil, fmt.Errorf("Error loading configured private key file: %s", err) } - signer, err := ssh.ParsePrivateKey([]byte(privateKey)) + signer, err := ssh.ParsePrivateKey(privateKey) if err != nil { return nil, fmt.Errorf("Error setting up SSH config: %s", err) } diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index acd9fd771..0a214199a 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -3,10 +3,11 @@ package digitalocean import ( "fmt" + "io/ioutil" + "github.com/digitalocean/godo" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" - "io/ioutil" ) type stepCreateDroplet struct { @@ -41,7 +42,7 @@ func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction { Slug: c.Image, }, SSHKeys: []godo.DropletCreateSSHKey{ - {ID: int(sshKeyId)}, + {ID: sshKeyId}, }, PrivateNetworking: c.PrivateNetworking, UserData: userData, diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index ae125259a..e563f89ea 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -254,7 +254,7 @@ func (d *HypervPS4Driver) verifyPSVersion() error { return err } - versionOutput := strings.TrimSpace(string(cmdOut)) + versionOutput := strings.TrimSpace(cmdOut) log.Printf("%s output: %s", versionCmd, versionOutput) ver, err := strconv.ParseInt(versionOutput, 10, 32) @@ -283,7 +283,7 @@ func (d *HypervPS4Driver) verifyPSHypervModule() error { return err } - res := strings.TrimSpace(string(cmdOut)) + res := strings.TrimSpace(cmdOut) if res == "False" { err := fmt.Errorf("%s", "PS Hyper-V module is not loaded. Make sure Hyper-V feature is on.") @@ -305,7 +305,7 @@ func (d *HypervPS4Driver) verifyHypervPermissions() error { return err } - res := strings.TrimSpace(string(cmdOut)) + res := strings.TrimSpace(cmdOut) if res == "False" { isAdmin, _ := powershell.IsCurrentUserAnAdministrator() diff --git a/builder/hyperv/common/step_configure_ip.go b/builder/hyperv/common/step_configure_ip.go index 90e17b238..330f784a0 100644 --- a/builder/hyperv/common/step_configure_ip.go +++ b/builder/hyperv/common/step_configure_ip.go @@ -2,11 +2,12 @@ package common import ( "fmt" - "github.com/mitchellh/multistep" - "github.com/mitchellh/packer/packer" "log" "strings" "time" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" ) type StepConfigureIp struct { @@ -35,7 +36,7 @@ func (s *StepConfigureIp) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - ip = strings.TrimSpace(string(cmdOut)) + ip = strings.TrimSpace(cmdOut) if ip != "False" { break diff --git a/builder/triton/access_config.go b/builder/triton/access_config.go index 05959e7b8..08deefdff 100644 --- a/builder/triton/access_config.go +++ b/builder/triton/access_config.go @@ -59,7 +59,7 @@ func (c *AccessConfig) CreateTritonClient() (*cloudapi.Client, error) { return nil, err } - userauth, err := auth.NewAuth(c.Account, string(keyData), "rsa-sha256") + userauth, err := auth.NewAuth(c.Account, keyData, "rsa-sha256") if err != nil { return nil, err } diff --git a/builder/virtualbox/common/ssh.go b/builder/virtualbox/common/ssh.go index b4a05c549..0ad514be9 100644 --- a/builder/virtualbox/common/ssh.go +++ b/builder/virtualbox/common/ssh.go @@ -15,7 +15,7 @@ func CommHost(host string) func(multistep.StateBag) (string, error) { func SSHPort(state multistep.StateBag) (int, error) { sshHostPort := state.Get("sshHostPort").(int) - return int(sshHostPort), nil + return sshHostPort, nil } func SSHConfigFunc(config SSHConfig) func(multistep.StateBag) (*gossh.ClientConfig, error) { diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index e57a98f0d..a674857b0 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -575,7 +575,7 @@ func GetExternalOnlineVirtualSwitch() (string, error) { var script = ` $adapters = Get-NetAdapter -Physical -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq 'Up' } | Sort-Object -Descending -Property Speed -foreach ($adapter in $adapters) { +foreach ($adapter in $adapters) { $switch = Get-VMSwitch -SwitchType External | Where-Object { $_.NetAdapterInterfaceDescription -eq $adapter.InterfaceDescription } if ($switch -ne $null) { @@ -605,10 +605,10 @@ $adapters = foreach ($name in $names) { Get-NetAdapter -Physical -Name $name -ErrorAction SilentlyContinue | where status -eq 'up' } -foreach ($adapter in $adapters) { +foreach ($adapter in $adapters) { $switch = Get-VMSwitch -SwitchType External | where { $_.NetAdapterInterfaceDescription -eq $adapter.InterfaceDescription } - if ($switch -eq $null) { + if ($switch -eq $null) { $switch = New-VMSwitch -Name $switchName -NetAdapterName $adapter.Name -AllowManagementOS $true -Notes 'Parent OS, VMs, WiFi' } @@ -617,9 +617,9 @@ foreach ($adapter in $adapters) { } } -if($switch -ne $null) { - Get-VMNetworkAdapter -VMName $vmName | Connect-VMNetworkAdapter -VMSwitch $switch -} else { +if($switch -ne $null) { + Get-VMNetworkAdapter -VMName $vmName | Connect-VMNetworkAdapter -VMSwitch $switch +} else { Write-Error 'No internet adapters found' } ` @@ -721,7 +721,7 @@ $vm.Uptime.TotalSeconds return 0, err } - uptime, err := strconv.ParseUint(strings.TrimSpace(string(cmdOut)), 10, 64) + uptime, err := strconv.ParseUint(strings.TrimSpace(cmdOut), 10, 64) return uptime, err } @@ -752,7 +752,7 @@ func IpAddress(mac string) (string, error) { param([string]$mac, [int]$addressIndex) try { $ip = Get-Vm | %{$_.NetworkAdapters} | ?{$_.MacAddress -eq $mac} | %{$_.IpAddresses[$addressIndex]} - + if($ip -eq $null) { return "" } @@ -807,7 +807,7 @@ func TypeScanCodes(vmName string, scanCodes string) error { param([string]$vmName, [string]$scanCodes) #Requires -Version 3 #Requires -RunAsAdministrator - + function Get-VMConsole { [CmdletBinding()] @@ -815,16 +815,16 @@ param([string]$vmName, [string]$scanCodes) [Parameter(Mandatory)] [string] $VMName ) - + $ErrorActionPreference = "Stop" - + $vm = Get-CimInstance -Namespace "root\virtualization\v2" -ClassName Msvm_ComputerSystem -ErrorAction Ignore -Verbose:$false | where ElementName -eq $VMName | select -first 1 if ($vm -eq $null){ Write-Error ("VirtualMachine({0}) is not found!" -f $VMName) } - + $vmKeyboard = $vm | Get-CimAssociatedInstance -ResultClassName "Msvm_Keyboard" -ErrorAction Ignore -Verbose:$false - + if ($vmKeyboard -eq $null) { $vmKeyboard = Get-CimInstance -Namespace "root\virtualization\v2" -ClassName Msvm_Keyboard -ErrorAction Ignore -Verbose:$false | where SystemName -eq $vm.Name | select -first 1 } @@ -832,22 +832,22 @@ param([string]$vmName, [string]$scanCodes) if ($vmKeyboard -eq $null) { $vmKeyboard = Get-CimInstance -Namespace "root\virtualization" -ClassName Msvm_Keyboard -ErrorAction Ignore -Verbose:$false | where SystemName -eq $vm.Name | select -first 1 } - + if ($vmKeyboard -eq $null){ Write-Error ("VirtualMachine({0}) keyboard class is not found!" -f $VMName) } - + #TODO: It may be better using New-Module -AsCustomObject to return console object? - + #Console object to return $console = [pscustomobject] @{ Msvm_ComputerSystem = $vm Msvm_Keyboard = $vmKeyboard } - + #Need to import assembly to use System.Windows.Input.Key Add-Type -AssemblyName WindowsBase - + #region Add Console Members $console | Add-Member -MemberType ScriptMethod -Name TypeText -Value { [OutputType([bool])] @@ -859,13 +859,13 @@ param([string]$vmName, [string]$scanCodes) $result = $this.Msvm_Keyboard | Invoke-CimMethod -MethodName "TypeText" -Arguments @{ asciiText = $AsciiText } return (0 -eq $result.ReturnValue) } - + #Define method:TypeCtrlAltDel $console | Add-Member -MemberType ScriptMethod -Name TypeCtrlAltDel -Value { $result = $this.Msvm_Keyboard | Invoke-CimMethod -MethodName "TypeCtrlAltDel" return (0 -eq $result.ReturnValue) } - + #Define method:TypeKey $console | Add-Member -MemberType ScriptMethod -Name TypeKey -Value { [OutputType([bool])] @@ -874,9 +874,9 @@ param([string]$vmName, [string]$scanCodes) [Windows.Input.Key] $Key, [Windows.Input.ModifierKeys] $ModifierKey = [Windows.Input.ModifierKeys]::None ) - + $keyCode = [Windows.Input.KeyInterop]::VirtualKeyFromKey($Key) - + switch ($ModifierKey) { ([Windows.Input.ModifierKeys]::Control){ $modifierKeyCode = [Windows.Input.KeyInterop]::VirtualKeyFromKey([Windows.Input.Key]::LeftCtrl)} @@ -884,7 +884,7 @@ param([string]$vmName, [string]$scanCodes) ([Windows.Input.ModifierKeys]::Shift){ $modifierKeyCode = [Windows.Input.KeyInterop]::VirtualKeyFromKey([Windows.Input.Key]::LeftShift)} ([Windows.Input.ModifierKeys]::Windows){ $modifierKeyCode = [Windows.Input.KeyInterop]::VirtualKeyFromKey([Windows.Input.Key]::LWin)} } - + if ($ModifierKey -eq [Windows.Input.ModifierKeys]::None) { $result = $this.Msvm_Keyboard | Invoke-CimMethod -MethodName "TypeKey" -Arguments @{ keyCode = $keyCode } @@ -897,7 +897,7 @@ param([string]$vmName, [string]$scanCodes) } $result = return (0 -eq $result.ReturnValue) } - + #Define method:Scancodes $console | Add-Member -MemberType ScriptMethod -Name TypeScancodes -Value { [OutputType([bool])] @@ -908,7 +908,7 @@ param([string]$vmName, [string]$scanCodes) $result = $this.Msvm_Keyboard | Invoke-CimMethod -MethodName "TypeScancodes" -Arguments @{ ScanCodes = $ScanCodes } return (0 -eq $result.ReturnValue) } - + #Define method:ExecCommand $console | Add-Member -MemberType ScriptMethod -Name ExecCommand -Value { param ( @@ -918,46 +918,46 @@ param([string]$vmName, [string]$scanCodes) if ([String]::IsNullOrEmpty($Command)){ return } - + $console.TypeText($Command) > $null $console.TypeKey([Windows.Input.Key]::Enter) > $null #sleep -Milliseconds 100 } - + #Define method:Dispose $console | Add-Member -MemberType ScriptMethod -Name Dispose -Value { $this.Msvm_ComputerSystem.Dispose() $this.Msvm_Keyboard.Dispose() } - - + + #endregion - + return $console } - + $vmConsole = Get-VMConsole -VMName $vmName $scanCodesToSend = '' $scanCodes.Split(' ') | %{ $scanCode = $_ - + if ($scanCode.StartsWith('wait')){ $timeToWait = $scanCode.Substring(4) if (!$timeToWait){ $timeToWait = "1" } - + if ($scanCodesToSend){ $scanCodesToSendByteArray = [byte[]]@($scanCodesToSend.Split(' ') | %{"0x$_"}) - + $scanCodesToSendByteArray | %{ $vmConsole.TypeScancodes($_) } } - + write-host "Special code found, will sleep $timeToWait second(s) at this point." Start-Sleep -s $timeToWait - + $scanCodesToSend = '' } else { if ($scanCodesToSend){ @@ -971,7 +971,7 @@ param([string]$vmName, [string]$scanCodes) } if ($scanCodesToSend){ $scanCodesToSendByteArray = [byte[]]@($scanCodesToSend.Split(' ') | %{"0x$_"}) - + $scanCodesToSendByteArray | %{ $vmConsole.TypeScancodes($_) } diff --git a/common/powershell/powershell.go b/common/powershell/powershell.go index 423bcd857..83190d1a4 100644 --- a/common/powershell/powershell.go +++ b/common/powershell/powershell.go @@ -219,7 +219,7 @@ param([string]$moduleName) return false, err } - res := strings.TrimSpace(string(cmdOut)) + res := strings.TrimSpace(cmdOut) if res == powerShellFalse { err := fmt.Errorf("PowerShell %s module is not loaded. Make sure %s feature is on.", moduleName, moduleName) diff --git a/communicator/ssh/password.go b/communicator/ssh/password.go index 8db6f82da..774be47f1 100644 --- a/communicator/ssh/password.go +++ b/communicator/ssh/password.go @@ -1,8 +1,9 @@ package ssh import ( - "golang.org/x/crypto/ssh" "log" + + "golang.org/x/crypto/ssh" ) // An implementation of ssh.KeyboardInteractiveChallenge that simply sends @@ -19,7 +20,7 @@ func PasswordKeyboardInteractive(password string) ssh.KeyboardInteractiveChallen // Just send the password back for all questions answers := make([]string, len(questions)) for i := range answers { - answers[i] = string(password) + answers[i] = password } return answers, nil diff --git a/provisioner/ansible/scp.go b/provisioner/ansible/scp.go index 03a8fe526..b36b22590 100644 --- a/provisioner/ansible/scp.go +++ b/provisioner/ansible/scp.go @@ -321,7 +321,7 @@ func scpResponse(r *bufio.Reader) error { // 1 is a warning. Anything higher (really just 2) is an error. if code > 1 { - return errors.New(string(message)) + return errors.New(message) } log.Println("WARNING:", err)