Skip to content

Commit 73e955f

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

File tree

17 files changed

+42
-112
lines changed

17 files changed

+42
-112
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: 4 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,9 @@ 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+
imgName := strings.Split(img, ":")[0]
179+
if slices.Contains(kicImagesRepo, imgName) {
180+
result = append(result, img)
182181
}
183182
}
184183
return result, nil

cmd/minikube/cmd/root.go

Lines changed: 2 additions & 8 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"
@@ -98,14 +99,7 @@ var RootCmd = &cobra.Command{
9899
func Execute() {
99100
// Check whether this is a windows binary (.exe) running inisde WSL.
100101
if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() {
101-
var found = false
102-
for _, a := range os.Args {
103-
if a == "--force" {
104-
found = true
105-
break
106-
}
107-
}
108-
if !found {
102+
if !slices.Contains(os.Args, "--force") {
109103
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")
110104
}
111105
}

cmd/minikube/cmd/start.go

Lines changed: 2 additions & 12 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"
@@ -1806,7 +1807,7 @@ func validateKubernetesVersion(old *config.ClusterConfig) {
18061807
}
18071808
if nvs.GT(newestVersion) {
18081809
out.WarningT("Specified Kubernetes version {{.specified}} is newer than the newest supported version: {{.newest}}. Use `minikube config defaults kubernetes-version` for details.", out.V{"specified": nvs, "newest": constants.NewestKubernetesVersion})
1809-
if contains(constants.ValidKubernetesVersions, kubernetesVer) {
1810+
if slices.Contains(constants.ValidKubernetesVersions, kubernetesVer) {
18101811
out.Styled(style.Check, "Kubernetes version {{.specified}} found in version list", out.V{"specified": nvs})
18111812
} else {
18121813
out.WarningT("Specified Kubernetes version {{.specified}} not found in Kubernetes version list", out.V{"specified": nvs})
@@ -2092,14 +2093,3 @@ func startNerdctld() {
20922093
exit.Error(reason.StartNerdctld, fmt.Sprintf("Failed to set up DOCKER_HOST: %s", rest.Output()), err)
20932094
}
20942095
}
2095-
2096-
// contains checks whether the parameter slice contains the parameter string
2097-
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
2105-
}

cmd/minikube/cmd/start_flags.go

Lines changed: 2 additions & 8 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,14 +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-
}
1026-
if !supported {
1020+
if !slices.Contains(supportedDrivers, driverName) {
10271021
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})
10281022
}
10291023
}

cmd/minikube/cmd/start_test.go

Lines changed: 2 additions & 8 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,14 +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-
}
263-
if !found {
257+
if !slices.Contains(config.DockerEnv, proxyEnv) {
264258
t.Fatalf("Value %s expected in dockerEnv but not occurred", test.proxy)
265259
}
266260
}

pkg/addons/validations.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package addons
1919
import (
2020
"errors"
2121
"fmt"
22+
"slices"
2223
"strconv"
2324

2425
"github.com/spf13/viper"
@@ -96,10 +97,5 @@ func isAddonValid(name string) (*Addon, bool) {
9697
}
9798

9899
func contains(slice []string, val string) bool {
99-
for _, item := range slice {
100-
if item == val {
101-
return true
102-
}
103-
}
104-
return false
100+
return slices.Contains(slice, val)
105101
}

pkg/minikube/audit/audit.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"os/user"
25+
"slices"
2526
"strings"
2627
"time"
2728

@@ -151,12 +152,7 @@ func shouldLog() bool {
151152
// commands that should not be logged.
152153
no := []string{"status", "version", "logs", "generate-docs", "profile"}
153154
a := pflag.Arg(0)
154-
for _, c := range no {
155-
if a == c {
156-
return false
157-
}
158-
}
159-
return true
155+
return !slices.Contains(no, a)
160156
}
161157

162158
// isDeletePurge return true if command is delete with purge flag.

pkg/minikube/cluster/pause.go

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

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

2223
"github.com/pkg/errors"
@@ -136,10 +137,5 @@ func doesNamespaceContainKubeSystem(namespaces []string) bool {
136137
if namespaces == nil {
137138
return true
138139
}
139-
for _, ns := range namespaces {
140-
if ns == "kube-system" {
141-
return true
142-
}
143-
}
144-
return false
140+
return slices.Contains(namespaces, "kube-system")
145141
}

pkg/minikube/config/extra_options.go

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

1919
import (
2020
"fmt"
21+
"slices"
2122
"strings"
2223

2324
"k8s.io/klog/v2"
@@ -144,12 +145,7 @@ func (cm ComponentExtraOptionMap) Get(component string) map[string]string {
144145
// ContainsParam checks if a given slice of strings contains the provided string.
145146
// If a modifier func is provided, it is called with the slice item before the comparison.
146147
func ContainsParam(slice []string, s string) bool {
147-
for _, item := range slice {
148-
if item == s {
149-
return true
150-
}
151-
}
152-
return false
148+
return slices.Contains(slice, s)
153149
}
154150

155151
// NewUnversionedOption returns a VersionedExtraOption that applies to all versions.

0 commit comments

Comments
 (0)