remove unnecessary type conversions

This commit is contained in:
Matthew Hooker 2017-03-28 18:47:10 -07:00
parent bf64d7bdc2
commit 35578d9ed1
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
13 changed files with 58 additions and 55 deletions

View File

@ -115,5 +115,5 @@ func GetInstanceMetaData(path string) (contents []byte, err error) {
if err != nil {
return
}
return []byte(body), err
return body, err
}

View File

@ -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 {

View File

@ -18,5 +18,5 @@ func GlueStrings(a, b string) string {
shift++
}
return string(a[:shift]) + b
return a[:shift] + b
}

View File

@ -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)
}

View File

@ -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,

View File

@ -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()

View File

@ -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

View File

@ -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
}

View File

@ -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) {

View File

@ -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 <wait> 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($_)
}

View File

@ -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)

View File

@ -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

View File

@ -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)