Skip to content

Commit 326733b

Browse files
committed
refactor: use slices.Contains to simplify code
Signed-off-by: deepdring <[email protected]>
1 parent 050e5ad commit 326733b

File tree

6 files changed

+15
-35
lines changed

6 files changed

+15
-35
lines changed

cmd/minikube/cmd/config/defaults_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"slices"
2021
"testing"
2122

2223
"k8s.io/minikube/pkg/minikube/out"
@@ -49,10 +50,8 @@ func TestGetDefaults(t *testing.T) {
4950
if tc.shouldErr {
5051
return
5152
}
52-
for _, d := range defaults {
53-
if d == tc.expectedContents {
54-
return
55-
}
53+
if slices.Contains(defaults, tc.expectedContents) {
54+
return
5655
}
5756
t.Fatalf("defaults didn't contain expected default. Actual: %v\nExpected: %v\n", defaults, tc.expectedContents)
5857
})

cmd/minikube/cmd/delete.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25+
"slices"
2526
"strconv"
2627
"strings"
2728
"time"
@@ -174,11 +175,8 @@ func kicbaseImages(ctx context.Context, ociBin string) ([]string, error) {
174175

175176
var result []string
176177
for _, img := range allImages {
177-
for _, kicImg := range kicImagesRepo {
178-
if kicImg == strings.Split(img, ":")[0] {
179-
result = append(result, img)
180-
break
181-
}
178+
if slices.Contains(kicImagesRepo, strings.Split(img, ":")[0]) {
179+
result = append(result, img)
182180
}
183181
}
184182
return result, nil

cmd/minikube/cmd/root.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"runtime"
25+
"slices"
2526
"strings"
2627

2728
"github.com/spf13/cobra"
@@ -99,11 +100,8 @@ func Execute() {
99100
// Check whether this is a windows binary (.exe) running inisde WSL.
100101
if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() {
101102
var found = false
102-
for _, a := range os.Args {
103-
if a == "--force" {
104-
found = true
105-
break
106-
}
103+
if slices.Contains(os.Args, "--force") {
104+
found = true
107105
}
108106
if !found {
109107
exit.Message(reason.WrongBinaryWSL, "You are trying to run a windows .exe binary inside WSL. For better integration please use a Linux binary instead (Download at https://minikube.sigs.k8s.io/docs/start/.). Otherwise if you still want to do this, you can do it using --force")

cmd/minikube/cmd/start.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os/user"
2929
"regexp"
3030
"runtime"
31+
"slices"
3132
"sort"
3233
"strconv"
3334
"strings"
@@ -2095,11 +2096,5 @@ func startNerdctld() {
20952096

20962097
// contains checks whether the parameter slice contains the parameter string
20972098
func contains(sl []string, s string) bool {
2098-
for _, k := range sl {
2099-
if s == k {
2100-
return true
2101-
}
2102-
2103-
}
2104-
return false
2099+
return slices.Contains(sl, s)
21052100
}

cmd/minikube/cmd/start_flags.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cmd
1919
import (
2020
"fmt"
2121
"runtime"
22+
"slices"
2223
"strings"
2324
"time"
2425

@@ -1016,13 +1017,7 @@ func checkExtraDiskOptions(cmd *cobra.Command, driverName string) {
10161017
supportedDrivers := []string{driver.HyperKit, driver.KVM2, driver.QEMU2, driver.VFKit, driver.Krunkit}
10171018

10181019
if cmd.Flags().Changed(extraDisks) {
1019-
supported := false
1020-
for _, driver := range supportedDrivers {
1021-
if driverName == driver {
1022-
supported = true
1023-
break
1024-
}
1025-
}
1020+
supported := slices.Contains(supportedDrivers, driverName)
10261021
if !supported {
10271022
out.WarningT("Specifying extra disks is currently only supported for the following drivers: {{.supported_drivers}}. If you can contribute to add this feature, please create a PR.", out.V{"supported_drivers": supportedDrivers})
10281023
}

cmd/minikube/cmd/start_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"slices"
2122
"strings"
2223
"testing"
2324
"time"
@@ -253,13 +254,7 @@ func TestGenerateCfgFromFlagsHTTPProxyHandling(t *testing.T) {
253254
}
254255
} else {
255256
// proxy must in config
256-
found := false
257-
for _, v := range config.DockerEnv {
258-
if v == proxyEnv {
259-
found = true
260-
break
261-
}
262-
}
257+
found := slices.Contains(config.DockerEnv, proxyEnv)
263258
if !found {
264259
t.Fatalf("Value %s expected in dockerEnv but not occurred", test.proxy)
265260
}

0 commit comments

Comments
 (0)