Skip to content

Commit a8b03c7

Browse files
committed
refactor: replace custom slice Contains method with slices.Contains
Replaced the custom implementation of the slice Contains method with built-in `slices.Contains` function from the Go standard library. This simplifies the code and improves readability and maintainability. Signed-off-by: Rohan Kumar <[email protected]>
1 parent dbd8038 commit a8b03c7

File tree

7 files changed

+11
-77
lines changed

7 files changed

+11
-77
lines changed

pkg/crc/cluster/clusteroperator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/http"
99
"net/url"
10+
"slices"
1011
"sort"
1112
"strings"
1213
"time"
@@ -17,7 +18,6 @@ import (
1718
"k8s.io/client-go/tools/clientcmd"
1819

1920
"github.com/crc-org/crc/v2/pkg/crc/logging"
20-
crcstrings "github.com/crc-org/crc/v2/pkg/strings"
2121
openshiftapi "github.com/openshift/api/config/v1"
2222
k8sapi "k8s.io/api/core/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -95,7 +95,7 @@ func getStatus(ctx context.Context, lister operatorLister, selector []string) (*
9595

9696
found := false
9797
for _, c := range co.Items {
98-
if len(selector) > 0 && !crcstrings.Contains(selector, c.ObjectMeta.Name) {
98+
if len(selector) > 0 && !slices.Contains(selector, c.ObjectMeta.Name) {
9999
continue
100100
}
101101
found = true

pkg/crc/machine/bundle/repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"runtime"
10+
"slices"
1011
"sort"
1112
"time"
1213

@@ -16,7 +17,6 @@ import (
1617
"github.com/crc-org/crc/v2/pkg/crc/logging"
1718
"github.com/crc-org/crc/v2/pkg/extract"
1819
crcos "github.com/crc-org/crc/v2/pkg/os"
19-
crcstrings "github.com/crc-org/crc/v2/pkg/strings"
2020
"github.com/pkg/errors"
2121
)
2222

@@ -58,7 +58,7 @@ func (repo *Repository) Get(bundleName string) (*CrcBundleInfo, error) {
5858
// TODO: update this logic after major release of bundle like 4.14
5959
// As of now we are using this logic to support older bundles of microshift and it need to be updated
6060
// to only provide app domain route information as per preset.
61-
if !crcstrings.Contains([]string{constants.AppsDomain, constants.MicroShiftAppDomain}, fmt.Sprintf(".%s", bundleInfo.ClusterInfo.AppsDomain)) {
61+
if !slices.Contains([]string{constants.AppsDomain, constants.MicroShiftAppDomain}, fmt.Sprintf(".%s", bundleInfo.ClusterInfo.AppsDomain)) {
6262
return nil, fmt.Errorf("unexpected bundle, it must have %s or %s apps domain", constants.AppsDomain, constants.MicroShiftAppDomain)
6363
}
6464
if bundleInfo.GetAPIHostname() != fmt.Sprintf("api%s", constants.ClusterDomain) {

pkg/crc/machine/kubeconfig.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/url"
1111
"os"
1212
"path/filepath"
13+
"slices"
1314
"strconv"
1415
"strings"
1516
"time"
@@ -242,14 +243,14 @@ func cleanKubeconfig(input, output string) error {
242243
var contextNames []string
243244
authNames := make(map[string]struct{})
244245
for name, context := range cfg.Contexts {
245-
if contains(clusterNames, context.Cluster) {
246+
if slices.Contains(clusterNames, context.Cluster) {
246247
contextNames = append(contextNames, name)
247248
authNames[context.AuthInfo] = struct{}{}
248249
}
249250
}
250251
// keep auth if it is shared with other contexts
251252
for _, context := range cfg.Contexts {
252-
if !contains(clusterNames, context.Cluster) {
253+
if !slices.Contains(clusterNames, context.Cluster) {
253254
delete(authNames, context.AuthInfo)
254255
}
255256
}
@@ -270,15 +271,6 @@ func cleanKubeconfig(input, output string) error {
270271
return clientcmd.WriteToFile(*cfg, output)
271272
}
272273

273-
func contains(arr []string, str string) bool {
274-
for _, a := range arr {
275-
if a == str {
276-
return true
277-
}
278-
}
279-
return false
280-
}
281-
282274
func mergeKubeConfigFile(kubeConfigFile string) error {
283275
return mergeConfigHelper(kubeConfigFile, getGlobalKubeConfigPath())
284276
}

pkg/crc/manpages/manpages_unix.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"os"
1111
"path/filepath"
12+
"slices"
1213
"strings"
1314

1415
"github.com/spf13/cobra/doc"
@@ -89,12 +90,7 @@ func appendToManPathEnvironmentVariable(folder string) error {
8990

9091
func manPathAlreadyContains(manPathEnvVarValue string, folder string) bool {
9192
manDirs := strings.Split(manPathEnvVarValue, string(os.PathListSeparator))
92-
for _, manDir := range manDirs {
93-
if manDir == folder {
94-
return true
95-
}
96-
}
97-
return false
93+
return slices.Contains(manDirs, folder)
9894
}
9995

10096
func removeFromManPathEnvironmentVariable(manPathEnvVarValue string, folder string) error {

pkg/crc/preflight/preflight_checks_network_linux.go

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

1213
"github.com/crc-org/crc/v2/pkg/crc/logging"
1314
"github.com/crc-org/crc/v2/pkg/crc/systemd"
1415
"github.com/crc-org/crc/v2/pkg/crc/systemd/states"
1516
crcos "github.com/crc-org/crc/v2/pkg/os"
16-
crcstring "github.com/crc-org/crc/v2/pkg/strings"
1717
)
1818

1919
var nmPreflightChecks = []Check{
@@ -287,7 +287,7 @@ func checkSystemdResolvedIsRunning() error {
287287
if err != nil {
288288
return err
289289
}
290-
if !crcstring.Contains(systemdResolvedManageResolvFilePath, rFilePath) {
290+
if !slices.Contains(systemdResolvedManageResolvFilePath, rFilePath) {
291291
return fmt.Errorf("%s is not managed by systemd-resolved", resolvFilePath)
292292
}
293293
return checkSystemdServiceRunning("systemd-resolved.service")

pkg/strings/strings.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ import (
55
"strings"
66
)
77

8-
func Contains(input []string, match string) bool {
9-
for _, v := range input {
10-
if v == match {
11-
return true
12-
}
13-
}
14-
return false
15-
}
16-
178
// Split a multi line string in an array of string, one for each line
189
func SplitLines(input string) []string {
1910
output := []string{}

pkg/strings/strings_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,6 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
type containsTest struct {
10-
input []string
11-
match string
12-
found bool
13-
}
14-
15-
var containsTests = map[string]containsTest{
16-
"Found": {
17-
input: []string{"a", "b", "c"},
18-
match: "b",
19-
found: true,
20-
},
21-
"NotFound": {
22-
input: []string{"a", "b", "c"},
23-
match: "d",
24-
found: false,
25-
},
26-
"OneElement": {
27-
input: []string{"a"},
28-
match: "a",
29-
found: true,
30-
},
31-
"EmptyArray": {
32-
input: []string{},
33-
match: "a",
34-
found: false,
35-
},
36-
"EmptyArrayEmptySearch": {
37-
input: []string{},
38-
match: "",
39-
found: false,
40-
},
41-
}
42-
43-
func TestContains(t *testing.T) {
44-
t.Run("Contains", func(t *testing.T) {
45-
for name, test := range containsTests {
46-
t.Run(name, func(t *testing.T) {
47-
found := Contains(test.input, test.match)
48-
assert.Equal(t, test.found, found)
49-
})
50-
}
51-
})
52-
}
53-
549
type splitLinesTest struct {
5510
input string
5611
splitOutput []string

0 commit comments

Comments
 (0)