From bcb1a367aa3bbd88fbc047217e27dbf1a66657d5 Mon Sep 17 00:00:00 2001 From: nolan emirot Date: Sat, 20 Apr 2024 18:47:26 -0400 Subject: [PATCH] refactor: string in slice part of stdlib now Signed-off-by: emirot --- kustomize/commands/create/create.go | 3 ++- kustomize/commands/edit/add/addbase.go | 4 +++- kustomize/commands/edit/add/addbase_test.go | 4 ++-- kustomize/commands/edit/add/addbuildmetadata.go | 3 ++- kustomize/commands/edit/add/addcomponent.go | 3 ++- kustomize/commands/edit/add/addgenerator.go | 3 ++- kustomize/commands/edit/add/addresource.go | 3 ++- kustomize/commands/edit/add/addtransformer.go | 3 ++- kustomize/commands/edit/fix/convert.go | 5 +++-- kustomize/commands/edit/remove/removebuildmetadata.go | 4 +++- kustomize/commands/edit/remove/removeconfigmap.go | 3 ++- kustomize/commands/edit/remove/removeresource.go | 3 ++- kustomize/commands/edit/remove/removesecret.go | 3 ++- kustomize/commands/edit/remove/removetransformer.go | 3 ++- .../commands/internal/kustfile/kustomizationfile.go | 10 ---------- kustomize/commands/internal/util/validate.go | 4 ++-- 16 files changed, 33 insertions(+), 28 deletions(-) diff --git a/kustomize/commands/create/create.go b/kustomize/commands/create/create.go index 81a05dbab0..6b867c9089 100644 --- a/kustomize/commands/create/create.go +++ b/kustomize/commands/create/create.go @@ -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) diff --git a/kustomize/commands/edit/add/addbase.go b/kustomize/commands/edit/add/addbase.go index 01a422e5a8..b33f128df2 100644 --- a/kustomize/commands/edit/add/addbase.go +++ b/kustomize/commands/edit/add/addbase.go @@ -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) diff --git a/kustomize/commands/edit/add/addbase_test.go b/kustomize/commands/edit/add/addbase_test.go index 50e5dd7fd4..2daf2e36c4 100644 --- a/kustomize/commands/edit/add/addbase_test.go +++ b/kustomize/commands/edit/add/addbase_test.go @@ -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)) } } diff --git a/kustomize/commands/edit/add/addbuildmetadata.go b/kustomize/commands/edit/add/addbuildmetadata.go index e4aa86ca33..d8af378424 100644 --- a/kustomize/commands/edit/add/addbuildmetadata.go +++ b/kustomize/commands/edit/add/addbuildmetadata.go @@ -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) diff --git a/kustomize/commands/edit/add/addcomponent.go b/kustomize/commands/edit/add/addcomponent.go index c0f728f717..a5078ae73d 100644 --- a/kustomize/commands/edit/add/addcomponent.go +++ b/kustomize/commands/edit/add/addcomponent.go @@ -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 } diff --git a/kustomize/commands/edit/add/addgenerator.go b/kustomize/commands/edit/add/addgenerator.go index ee475d1b47..ab1d17230d 100644 --- a/kustomize/commands/edit/add/addgenerator.go +++ b/kustomize/commands/edit/add/addgenerator.go @@ -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 } diff --git a/kustomize/commands/edit/add/addresource.go b/kustomize/commands/edit/add/addresource.go index 529f2b1194..3c63b4b139 100644 --- a/kustomize/commands/edit/add/addresource.go +++ b/kustomize/commands/edit/add/addresource.go @@ -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 } diff --git a/kustomize/commands/edit/add/addtransformer.go b/kustomize/commands/edit/add/addtransformer.go index 288ee3f3ed..ffc4aa77ea 100644 --- a/kustomize/commands/edit/add/addtransformer.go +++ b/kustomize/commands/edit/add/addtransformer.go @@ -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 } diff --git a/kustomize/commands/edit/fix/convert.go b/kustomize/commands/edit/fix/convert.go index f6c4508030..78258ce298 100644 --- a/kustomize/commands/edit/fix/convert.go +++ b/kustomize/commands/edit/fix/convert.go @@ -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 } diff --git a/kustomize/commands/edit/remove/removebuildmetadata.go b/kustomize/commands/edit/remove/removebuildmetadata.go index de27de0a18..58e30488ce 100644 --- a/kustomize/commands/edit/remove/removebuildmetadata.go +++ b/kustomize/commands/edit/remove/removebuildmetadata.go @@ -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) } } diff --git a/kustomize/commands/edit/remove/removeconfigmap.go b/kustomize/commands/edit/remove/removeconfigmap.go index 7eeedb7d0f..056db08e9e 100644 --- a/kustomize/commands/edit/remove/removeconfigmap.go +++ b/kustomize/commands/edit/remove/removeconfigmap.go @@ -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 diff --git a/kustomize/commands/edit/remove/removeresource.go b/kustomize/commands/edit/remove/removeresource.go index fc76943d00..1fdbf79be1 100644 --- a/kustomize/commands/edit/remove/removeresource.go +++ b/kustomize/commands/edit/remove/removeresource.go @@ -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) diff --git a/kustomize/commands/edit/remove/removesecret.go b/kustomize/commands/edit/remove/removesecret.go index 89a8af2306..d648051ed5 100644 --- a/kustomize/commands/edit/remove/removesecret.go +++ b/kustomize/commands/edit/remove/removesecret.go @@ -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 diff --git a/kustomize/commands/edit/remove/removetransformer.go b/kustomize/commands/edit/remove/removetransformer.go index c8e188cecb..7de87256f9 100644 --- a/kustomize/commands/edit/remove/removetransformer.go +++ b/kustomize/commands/edit/remove/removetransformer.go @@ -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) diff --git a/kustomize/commands/internal/kustfile/kustomizationfile.go b/kustomize/commands/internal/kustfile/kustomizationfile.go index 4804b56919..1f9e5f22bb 100644 --- a/kustomize/commands/internal/kustfile/kustomizationfile.go +++ b/kustomize/commands/internal/kustfile/kustomizationfile.go @@ -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 diff --git a/kustomize/commands/internal/util/validate.go b/kustomize/commands/internal/util/validate.go index bf041b50f1..0fde88f784 100644 --- a/kustomize/commands/internal/util/validate.go +++ b/kustomize/commands/internal/util/validate.go @@ -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) } }