Skip to content

Commit

Permalink
Merge pull request #5671 from emirot/refactor/string_inSlice
Browse files Browse the repository at this point in the history
refactor: string in slice is now part of stdlib
  • Loading branch information
k8s-ci-robot authored Apr 23, 2024
2 parents 7f99ceb + bcb1a36 commit 2e6171a
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 28 deletions.
3 changes: 2 additions & 1 deletion kustomize/commands/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -113,7 +114,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory)
return err
}
for _, resource := range detected {
if kustfile.StringInSlice(resource, resources) {
if slices.Contains(resources, resource) {
continue
}
resources = append(resources, resource)
Expand Down
4 changes: 3 additions & 1 deletion kustomize/commands/edit/add/addbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"strings"

"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kyaml/filesys"
Expand Down Expand Up @@ -64,7 +66,7 @@ func (o *addBaseOptions) RunAddBase(fSys filesys.FileSystem) error {
if !fSys.Exists(path) {
return errors.New(path + " does not exist")
}
if kustfile.StringInSlice(path, m.Resources) {
if slices.Contains(m.Resources, path) {
return fmt.Errorf("base %s already in kustomization file", path)
}
m.Resources = append(m.Resources, path)
Expand Down
4 changes: 2 additions & 2 deletions kustomize/commands/edit/add/addbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
package add

import (
"slices"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestAddBaseAlreadyThere(t *testing.T) {
for _, base := range bases {
msg := "base " + base + " already in kustomization file"
expectedErrors = append(expectedErrors, msg)
assert.True(t, kustfile.StringInSlice(msg, expectedErrors))
assert.True(t, slices.Contains(expectedErrors, msg))
}
}

Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/add/addbuildmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package add

import (
"fmt"
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (o *addBuildMetadataOptions) RunAddBuildMetadata(fSys filesys.FileSystem) e
return err
}
for _, opt := range o.buildMetadataOptions {
if kustfile.StringInSlice(opt, m.BuildMetadata) {
if slices.Contains(m.BuildMetadata, opt) {
return fmt.Errorf("buildMetadata option %s already in kustomization file", opt)
}
m.BuildMetadata = append(m.BuildMetadata, opt)
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/add/addcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/pkg/loader"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (o *addComponentOptions) RunAddComponent(fSys filesys.FileSystem) error {

for _, component := range components {
if mf.GetPath() != component {
if kustfile.StringInSlice(component, m.Components) {
if slices.Contains(m.Components, component) {
log.Printf("component %s already in kustomization file", component)
continue
}
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/add/addgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (o *addGeneratorOptions) RunAddGenerator(fSys filesys.FileSystem) error {
return err
}
for _, t := range o.generatorFilePaths {
if kustfile.StringInSlice(t, m.Generators) {
if slices.Contains(m.Generators, t) {
log.Printf("generator %s already in kustomization file", t)
continue
}
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/add/addresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"

"github.com/spf13/cobra"
ldrhelper "sigs.k8s.io/kustomize/api/pkg/loader"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error {

for _, resource := range resources {
if mf.GetPath() != resource {
if kustfile.StringInSlice(resource, m.Resources) {
if slices.Contains(m.Resources, resource) {
log.Printf("resource %s already in kustomization file", resource)
continue
}
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/add/addtransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (o *addTransformerOptions) RunAddTransformer(fSys filesys.FileSystem) error
return err
}
for _, t := range o.transformerFilePaths {
if kustfile.StringInSlice(t, m.Transformers) {
if slices.Contains(m.Transformers, t) {
log.Printf("transformer %s already in kustomization file", t)
continue
}
Expand Down
5 changes: 3 additions & 2 deletions kustomize/commands/edit/fix/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"path"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -55,11 +56,11 @@ func filesTouchedByKustomize(k *types.Kustomization, filepath string, fSys files
files, err := fSys.ReadDir(r)
if err == nil && len(files) > 0 {
for _, file := range files {
if !stringInSlice(file, []string{
if !slices.Contains([]string{
"kustomization.yaml",
"kustomization.yml",
"Kustomization",
}) {
}, file) {
continue
}

Expand Down
4 changes: 3 additions & 1 deletion kustomize/commands/edit/remove/removebuildmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package remove

import (
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/util"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (o *removeBuildMetadataOptions) RunRemoveBuildMetadata(fSys filesys.FileSys
}
var newOptions []string
for _, opt := range m.BuildMetadata {
if !kustfile.StringInSlice(opt, o.buildMetadataOptions) {
if !slices.Contains(o.buildMetadataOptions, opt) {
newOptions = append(newOptions, opt)
}
}
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/remove/removeconfigmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"log"
"slices"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (o *removeConfigMapOptions) RunRemoveConfigMap(fSys filesys.FileSystem) err
remainingConfigMaps := make([]types.ConfigMapArgs, 0, len(m.ConfigMapGenerator))

for _, currentConfigMap := range m.ConfigMapGenerator {
if kustfile.StringInSlice(currentConfigMap.Name, o.configMapNamesToRemove) &&
if slices.Contains(o.configMapNamesToRemove, currentConfigMap.Name) &&
util.NamespaceEqual(currentConfigMap.Namespace, o.namespace) {
foundConfigMaps[currentConfigMap.Name] = struct{}{}
continue
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/remove/removeresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package remove
import (
"errors"
"path/filepath"
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (o *removeResourceOptions) RunRemoveResource(fSys filesys.FileSystem) error

newResources := make([]string, 0, len(m.Resources))
for _, resource := range m.Resources {
if kustfile.StringInSlice(resource, resources) {
if slices.Contains(resources, resource) {
continue
}
newResources = append(newResources, resource)
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/remove/removesecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"log"
"slices"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (o *removeSecretOptions) RunRemoveSecret(fSys filesys.FileSystem) error {
remainingSecrets := make([]types.SecretArgs, 0, len(m.SecretGenerator))

for _, currentSecret := range m.SecretGenerator {
if kustfile.StringInSlice(currentSecret.Name, o.secretNamesToRemove) &&
if slices.Contains(o.secretNamesToRemove, currentSecret.Name) &&
util.NamespaceEqual(currentSecret.Namespace, o.namespace) {
foundSecrets[currentSecret.Name] = struct{}{}
continue
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/edit/remove/removetransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package remove

import (
"errors"
"slices"

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (o *removeTransformerOptions) RunRemoveTransformer(fSys filesys.FileSystem)

newTransformers := make([]string, 0, len(m.Transformers))
for _, transformer := range m.Transformers {
if kustfile.StringInSlice(transformer, transformers) {
if slices.Contains(transformers, transformer) {
continue
}
newTransformers = append(newTransformers, transformer)
Expand Down
10 changes: 0 additions & 10 deletions kustomize/commands/internal/kustfile/kustomizationfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,6 @@ func (mf *kustomizationFile) Write(kustomization *types.Kustomization) error {
return mf.fSys.WriteFile(mf.path, data)
}

// StringInSlice returns true if the string is in the slice.
func StringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}

func (mf *kustomizationFile) parseCommentedFields(content []byte) error {
buffer := bytes.NewBuffer(content)
var comments [][]byte
Expand Down
4 changes: 2 additions & 2 deletions kustomize/commands/internal/util/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package util
import (
"errors"
"fmt"
"slices"
"strings"

"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
)

type BuildMetadataValidator struct{}
Expand All @@ -23,7 +23,7 @@ func (b *BuildMetadataValidator) Validate(args []string) ([]string, error) {
}
opts := strings.Split(args[0], ",")
for _, opt := range opts {
if !kustfile.StringInSlice(opt, types.BuildMetadataOptions) {
if !slices.Contains(types.BuildMetadataOptions, opt) {
return nil, fmt.Errorf("invalid buildMetadata option: %s", opt)
}
}
Expand Down

0 comments on commit 2e6171a

Please sign in to comment.