From c7a74345476f1766b1aff59bc6542eeecb0baf3b Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Tue, 4 Feb 2025 19:30:32 +0530 Subject: [PATCH] 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 --- pkg/crc/cluster/clusteroperator.go | 4 +- pkg/crc/machine/bundle/repository.go | 4 +- pkg/crc/machine/kubeconfig.go | 14 ++---- pkg/crc/manpages/manpages_unix.go | 8 +--- .../preflight_checks_network_linux.go | 4 +- pkg/strings/strings.go | 9 ---- pkg/strings/strings_test.go | 45 ------------------- 7 files changed, 11 insertions(+), 77 deletions(-) diff --git a/pkg/crc/cluster/clusteroperator.go b/pkg/crc/cluster/clusteroperator.go index 719f801b4a..bc4d762917 100644 --- a/pkg/crc/cluster/clusteroperator.go +++ b/pkg/crc/cluster/clusteroperator.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "net/url" + "slices" "sort" "strings" "time" @@ -17,7 +18,6 @@ import ( "k8s.io/client-go/tools/clientcmd" "github.com/crc-org/crc/v2/pkg/crc/logging" - crcstrings "github.com/crc-org/crc/v2/pkg/strings" openshiftapi "github.com/openshift/api/config/v1" k8sapi "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -95,7 +95,7 @@ func getStatus(ctx context.Context, lister operatorLister, selector []string) (* found := false for _, c := range co.Items { - if len(selector) > 0 && !crcstrings.Contains(selector, c.ObjectMeta.Name) { + if len(selector) > 0 && !slices.Contains(selector, c.ObjectMeta.Name) { continue } found = true diff --git a/pkg/crc/machine/bundle/repository.go b/pkg/crc/machine/bundle/repository.go index 46ea2bf424..02bc871a77 100644 --- a/pkg/crc/machine/bundle/repository.go +++ b/pkg/crc/machine/bundle/repository.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "sort" "time" @@ -16,7 +17,6 @@ import ( "github.com/crc-org/crc/v2/pkg/crc/logging" "github.com/crc-org/crc/v2/pkg/extract" crcos "github.com/crc-org/crc/v2/pkg/os" - crcstrings "github.com/crc-org/crc/v2/pkg/strings" "github.com/pkg/errors" ) @@ -58,7 +58,7 @@ func (repo *Repository) Get(bundleName string) (*CrcBundleInfo, error) { // TODO: update this logic after major release of bundle like 4.14 // As of now we are using this logic to support older bundles of microshift and it need to be updated // to only provide app domain route information as per preset. - if !crcstrings.Contains([]string{constants.AppsDomain, constants.MicroShiftAppDomain}, fmt.Sprintf(".%s", bundleInfo.ClusterInfo.AppsDomain)) { + if !slices.Contains([]string{constants.AppsDomain, constants.MicroShiftAppDomain}, fmt.Sprintf(".%s", bundleInfo.ClusterInfo.AppsDomain)) { return nil, fmt.Errorf("unexpected bundle, it must have %s or %s apps domain", constants.AppsDomain, constants.MicroShiftAppDomain) } if bundleInfo.GetAPIHostname() != fmt.Sprintf("api%s", constants.ClusterDomain) { diff --git a/pkg/crc/machine/kubeconfig.go b/pkg/crc/machine/kubeconfig.go index 6f685ca066..e2e12e0280 100644 --- a/pkg/crc/machine/kubeconfig.go +++ b/pkg/crc/machine/kubeconfig.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strconv" "strings" "time" @@ -242,14 +243,14 @@ func cleanKubeconfig(input, output string) error { var contextNames []string authNames := make(map[string]struct{}) for name, context := range cfg.Contexts { - if contains(clusterNames, context.Cluster) { + if slices.Contains(clusterNames, context.Cluster) { contextNames = append(contextNames, name) authNames[context.AuthInfo] = struct{}{} } } // keep auth if it is shared with other contexts for _, context := range cfg.Contexts { - if !contains(clusterNames, context.Cluster) { + if !slices.Contains(clusterNames, context.Cluster) { delete(authNames, context.AuthInfo) } } @@ -270,15 +271,6 @@ func cleanKubeconfig(input, output string) error { return clientcmd.WriteToFile(*cfg, output) } -func contains(arr []string, str string) bool { - for _, a := range arr { - if a == str { - return true - } - } - return false -} - func mergeKubeConfigFile(kubeConfigFile string) error { return mergeConfigHelper(kubeConfigFile, getGlobalKubeConfigPath()) } diff --git a/pkg/crc/manpages/manpages_unix.go b/pkg/crc/manpages/manpages_unix.go index cf4180fdda..6110f91e3e 100644 --- a/pkg/crc/manpages/manpages_unix.go +++ b/pkg/crc/manpages/manpages_unix.go @@ -9,6 +9,7 @@ import ( "io" "os" "path/filepath" + "slices" "strings" "github.com/spf13/cobra/doc" @@ -89,12 +90,7 @@ func appendToManPathEnvironmentVariable(folder string) error { func manPathAlreadyContains(manPathEnvVarValue string, folder string) bool { manDirs := strings.Split(manPathEnvVarValue, string(os.PathListSeparator)) - for _, manDir := range manDirs { - if manDir == folder { - return true - } - } - return false + return slices.Contains(manDirs, folder) } func removeFromManPathEnvironmentVariable(manPathEnvVarValue string, folder string) error { diff --git a/pkg/crc/preflight/preflight_checks_network_linux.go b/pkg/crc/preflight/preflight_checks_network_linux.go index 9bd2287228..c0a9c484f4 100644 --- a/pkg/crc/preflight/preflight_checks_network_linux.go +++ b/pkg/crc/preflight/preflight_checks_network_linux.go @@ -8,12 +8,12 @@ import ( "os" "os/exec" "path/filepath" + "slices" "github.com/crc-org/crc/v2/pkg/crc/logging" "github.com/crc-org/crc/v2/pkg/crc/systemd" "github.com/crc-org/crc/v2/pkg/crc/systemd/states" crcos "github.com/crc-org/crc/v2/pkg/os" - crcstring "github.com/crc-org/crc/v2/pkg/strings" ) var nmPreflightChecks = []Check{ @@ -287,7 +287,7 @@ func checkSystemdResolvedIsRunning() error { if err != nil { return err } - if !crcstring.Contains(systemdResolvedManageResolvFilePath, rFilePath) { + if !slices.Contains(systemdResolvedManageResolvFilePath, rFilePath) { return fmt.Errorf("%s is not managed by systemd-resolved", resolvFilePath) } return checkSystemdServiceRunning("systemd-resolved.service") diff --git a/pkg/strings/strings.go b/pkg/strings/strings.go index 28af3b79c0..888d25cbac 100644 --- a/pkg/strings/strings.go +++ b/pkg/strings/strings.go @@ -5,15 +5,6 @@ import ( "strings" ) -func Contains(input []string, match string) bool { - for _, v := range input { - if v == match { - return true - } - } - return false -} - // Split a multi line string in an array of string, one for each line func SplitLines(input string) []string { output := []string{} diff --git a/pkg/strings/strings_test.go b/pkg/strings/strings_test.go index 08c37e931a..6086ca74bd 100644 --- a/pkg/strings/strings_test.go +++ b/pkg/strings/strings_test.go @@ -6,51 +6,6 @@ import ( "github.com/stretchr/testify/assert" ) -type containsTest struct { - input []string - match string - found bool -} - -var containsTests = map[string]containsTest{ - "Found": { - input: []string{"a", "b", "c"}, - match: "b", - found: true, - }, - "NotFound": { - input: []string{"a", "b", "c"}, - match: "d", - found: false, - }, - "OneElement": { - input: []string{"a"}, - match: "a", - found: true, - }, - "EmptyArray": { - input: []string{}, - match: "a", - found: false, - }, - "EmptyArrayEmptySearch": { - input: []string{}, - match: "", - found: false, - }, -} - -func TestContains(t *testing.T) { - t.Run("Contains", func(t *testing.T) { - for name, test := range containsTests { - t.Run(name, func(t *testing.T) { - found := Contains(test.input, test.match) - assert.Equal(t, test.found, found) - }) - } - }) -} - type splitLinesTest struct { input string splitOutput []string