Skip to content

Commit

Permalink
refactor: replace custom slice Contains method with slices.Contains
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rohanKanojia authored and praveenkumar committed Feb 5, 2025
1 parent ac09b23 commit c7a7434
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 77 deletions.
4 changes: 2 additions & 2 deletions pkg/crc/cluster/clusteroperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"net/url"
"slices"
"sort"
"strings"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/machine/bundle/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"sort"
"time"

Expand All @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 3 additions & 11 deletions pkg/crc/machine/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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())
}
Expand Down
8 changes: 2 additions & 6 deletions pkg/crc/manpages/manpages_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"

"github.com/spf13/cobra/doc"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/preflight/preflight_checks_network_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 0 additions & 9 deletions pkg/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
45 changes: 0 additions & 45 deletions pkg/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c7a7434

Please sign in to comment.