Merge pull request #5671 from emirot/refactor/string_inSlice

refactor: string in slice is now part of stdlib
This commit is contained in:
Kubernetes Prow Robot
2024-04-23 04:36:33 -07:00
committed by GitHub
16 changed files with 33 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"github.com/spf13/cobra"
@@ -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)

View File

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

View File

@@ -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"
)
@@ -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))
}
}

View File

@@ -5,6 +5,7 @@ package add
import (
"fmt"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -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)

View File

@@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/pkg/loader"
@@ -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
}

View File

@@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -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
}

View File

@@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"
"github.com/spf13/cobra"
ldrhelper "sigs.k8s.io/kustomize/api/pkg/loader"
@@ -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
}

View File

@@ -6,6 +6,7 @@ package add
import (
"errors"
"log"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -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
}

View File

@@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"path"
"slices"
"strconv"
"strings"
@@ -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
}

View File

@@ -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"
@@ -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)
}
}

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"log"
"slices"
"strings"
"github.com/spf13/cobra"
@@ -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

View File

@@ -6,6 +6,7 @@ package remove
import (
"errors"
"path/filepath"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
@@ -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)

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"log"
"slices"
"strings"
"github.com/spf13/cobra"
@@ -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

View File

@@ -5,6 +5,7 @@ package remove
import (
"errors"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
@@ -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)

View File

@@ -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

View File

@@ -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{}
@@ -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)
}
}