Skip to content

Commit f119b5c

Browse files
authored
refactor: use slices.Contains for membership checks (#515)
Replaces manual loops that checked for membership with `slices.Contains` for clarity and brevity. Behavior unchanged; this is a readability/maintenance refactor. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 028a955 commit f119b5c

5 files changed

Lines changed: 13 additions & 27 deletions

File tree

builder/vmware/common/cdrom_utils.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package common
77
import (
88
"fmt"
99
"regexp"
10+
"slices"
1011
"strconv"
1112
"strings"
1213
)
@@ -21,13 +22,7 @@ func FindNextAvailableCDROMSlot(vmxData map[string]string, adapterType string) (
2122
adapterType = strings.ToLower(adapterType)
2223

2324
validAdapters := []string{"ide", "sata", "scsi"}
24-
isValid := false
25-
for _, valid := range validAdapters {
26-
if adapterType == valid {
27-
isValid = true
28-
break
29-
}
30-
}
25+
isValid := slices.Contains(validAdapters, adapterType)
3126
if !isValid {
3227
return "", fmt.Errorf("invalid adapter type: %s; must be one of %v", adapterType, validAdapters)
3328
}

builder/vmware/common/driver_fusion.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os/exec"
1414
"path/filepath"
1515
"runtime"
16+
"slices"
1617
"strings"
1718

1819
"github.com/hashicorp/go-version"
@@ -98,10 +99,8 @@ func (d *FusionDriver) IsRunning(vmxPath string) (bool, error) {
9899
return false, err
99100
}
100101

101-
for _, line := range strings.Split(stdout, "\n") {
102-
if line == absVmxPath {
103-
return true, nil
104-
}
102+
if slices.Contains(strings.Split(stdout, "\n"), absVmxPath) {
103+
return true, nil
105104
}
106105

107106
return false, nil

builder/vmware/common/driver_workstation.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os/exec"
1515
"path/filepath"
1616
"runtime"
17+
"slices"
1718
"sort"
1819
"strings"
1920

@@ -176,10 +177,8 @@ func (d *WorkstationDriver) IsRunning(vmxPath string) (bool, error) {
176177
return false, err
177178
}
178179

179-
for _, line := range strings.Split(stdout, "\n") {
180-
if line == vmxPath {
181-
return true, nil
182-
}
180+
if slices.Contains(strings.Split(stdout, "\n"), vmxPath) {
181+
return true, nil
183182
}
184183

185184
return false, nil

builder/vmware/common/step_attach_tools_cdrom_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"os"
1010
"path/filepath"
11+
"slices"
1112
"testing"
1213

1314
"github.com/hashicorp/packer-plugin-sdk/multistep"
@@ -97,13 +98,7 @@ displayName = "test"
9798
}
9899

99100
tmpDevices := state.Get("temporaryDevices").([]string)
100-
found := false
101-
for _, device := range tmpDevices {
102-
if device == "ide0:0" {
103-
found = true
104-
break
105-
}
106-
}
101+
found := slices.Contains(tmpDevices, "ide0:0")
107102
if !found {
108103
t.Fatal("tools CD-ROM device should be added to temporaryDevices")
109104
}

builder/vmware/common/step_clean_files.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"os"
1010
"path/filepath"
11+
"slices"
1112

1213
"github.com/hashicorp/packer-plugin-sdk/multistep"
1314
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
@@ -33,11 +34,8 @@ func (StepCleanFiles) Run(ctx context.Context, state multistep.StateBag) multist
3334
// virtual machine, we get rid of it.
3435
keep := false
3536
ext := filepath.Ext(path)
36-
for _, goodExt := range skipCleanFileExtensions {
37-
if goodExt == ext {
38-
keep = true
39-
break
40-
}
37+
if slices.Contains(skipCleanFileExtensions, ext) {
38+
keep = true
4139
}
4240

4341
if !keep {

0 commit comments

Comments
 (0)