refactor: use slices.Contains to simplify code

Signed-off-by: findnature <cricis@aliyun.com>
pull/30827/head
findnature 4 months ago
parent 31e22b9866
commit ac8d2f9aed

@ -189,12 +189,7 @@ func (cfg *Configuration) deleteHooksByPolicy(hooks []*release.Hook, policy rele
// hookHasDeletePolicy determines whether the defined hook deletion policy matches the hook deletion polices // hookHasDeletePolicy determines whether the defined hook deletion policy matches the hook deletion polices
// supported by helm. If so, mark the hook as one should be deleted. // supported by helm. If so, mark the hook as one should be deleted.
func hookHasDeletePolicy(h *release.Hook, policy release.HookDeletePolicy) bool { func hookHasDeletePolicy(h *release.Hook, policy release.HookDeletePolicy) bool {
for _, v := range h.DeletePolicies { return slices.Contains(h.DeletePolicies, policy)
if policy == v {
return true
}
}
return false
} }
// outputLogsByPolicy outputs a pods logs if the hook policy instructs it to // outputLogsByPolicy outputs a pods logs if the hook policy instructs it to

@ -17,6 +17,7 @@ package util
import ( import (
"fmt" "fmt"
"slices"
"strconv" "strconv"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
@ -102,12 +103,7 @@ type VersionSet []string
// //
// vs.Has("apps/v1") // vs.Has("apps/v1")
func (v VersionSet) Has(apiVersion string) bool { func (v VersionSet) Has(apiVersion string) bool {
for _, x := range v { return slices.Contains(v, apiVersion)
if x == apiVersion {
return true
}
}
return false
} }
func allKnownVersions() VersionSet { func allKnownVersions() VersionSet {

@ -25,6 +25,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"os/signal" "os/signal"
"slices"
"syscall" "syscall"
"time" "time"
@ -350,13 +351,7 @@ func compInstall(args []string, toComplete string, client *action.Install) ([]st
func validateDryRunOptionFlag(dryRunOptionFlagValue string) error { func validateDryRunOptionFlag(dryRunOptionFlagValue string) error {
// Validate dry-run flag value with a set of allowed value // Validate dry-run flag value with a set of allowed value
allowedDryRunValues := []string{"false", "true", "none", "client", "server"} allowedDryRunValues := []string{"false", "true", "none", "client", "server"}
isAllowed := false isAllowed := slices.Contains(allowedDryRunValues, dryRunOptionFlagValue)
for _, v := range allowedDryRunValues {
if dryRunOptionFlagValue == v {
isAllowed = true
break
}
}
if !isAllowed { if !isAllowed {
return errors.New("invalid dry-run flag. Flag must one of the following: false, true, none, client, server") return errors.New("invalid dry-run flag. Flag must one of the following: false, true, none, client, server")
} }

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"slices"
"strconv" "strconv"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
@ -203,13 +204,7 @@ func filterReleases(releases []*release.Release, ignoredReleaseNames []string) [
var filteredReleases []*release.Release var filteredReleases []*release.Release
for _, rel := range releases { for _, rel := range releases {
found := false found := slices.Contains(ignoredReleaseNames, rel.Name)
for _, ignoredName := range ignoredReleaseNames {
if rel.Name == ignoredName {
found = true
break
}
}
if !found { if !found {
filteredReleases = append(filteredReleases, rel) filteredReleases = append(filteredReleases, rel)
} }

@ -23,6 +23,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"slices"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@ -163,10 +164,8 @@ func manuallyProcessArgs(args []string) ([]string, []string) {
} }
isKnown := func(v string) string { isKnown := func(v string) string {
for _, i := range kvargs { if slices.Contains(kvargs, v) {
if i == v { return v
return v
}
} }
return "" return ""
} }

@ -19,6 +19,7 @@ import (
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"slices"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -60,13 +61,7 @@ func filterPlugins(plugins []*plugin.Plugin, ignoredPluginNames []string) []*plu
var filteredPlugins []*plugin.Plugin var filteredPlugins []*plugin.Plugin
for _, plugin := range plugins { for _, plugin := range plugins {
found := false found := slices.Contains(ignoredPluginNames, plugin.Metadata.Name)
for _, ignoredName := range ignoredPluginNames {
if plugin.Metadata.Name == ignoredName {
found = true
break
}
}
if !found { if !found {
filteredPlugins = append(filteredPlugins, plugin) filteredPlugins = append(filteredPlugins, plugin)
} }

@ -20,6 +20,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"net/http" "net/http"
"slices"
"time" "time"
"helm.sh/helm/v4/pkg/cli" "helm.sh/helm/v4/pkg/cli"
@ -163,12 +164,7 @@ type Provider struct {
// Provides returns true if the given scheme is supported by this Provider. // Provides returns true if the given scheme is supported by this Provider.
func (p Provider) Provides(scheme string) bool { func (p Provider) Provides(scheme string) bool {
for _, i := range p.Schemes { return slices.Contains(p.Schemes, scheme)
if i == scheme {
return true
}
}
return false
} }
// Providers is a collection of Provider objects. // Providers is a collection of Provider objects.

@ -25,6 +25,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"slices"
"strings" "strings"
"k8s.io/apimachinery/pkg/api/validation" "k8s.io/apimachinery/pkg/api/validation"
@ -206,10 +207,8 @@ func validateAllowedExtension(fileName string) error {
ext := filepath.Ext(fileName) ext := filepath.Ext(fileName)
validExtensions := []string{".yaml", ".yml", ".tpl", ".txt"} validExtensions := []string{".yaml", ".yml", ".tpl", ".txt"}
for _, b := range validExtensions { if slices.Contains(validExtensions, ext) {
if b == ext { return nil
return nil
}
} }
return fmt.Errorf("file extension '%s' not valid. Valid extensions are .yaml, .yml, .tpl, or .txt", ext) return fmt.Errorf("file extension '%s' not valid. Valid extensions are .yaml, .yml, .tpl, or .txt", ext)

@ -27,6 +27,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"slices"
"strings" "strings"
securejoin "github.com/cyphar/filepath-securejoin" securejoin "github.com/cyphar/filepath-securejoin"
@ -196,10 +197,8 @@ func cleanJoin(root, dest string) (string, error) {
// We want to alert the user that something bad was attempted. Cleaning it // We want to alert the user that something bad was attempted. Cleaning it
// is not a good practice. // is not a good practice.
for _, part := range strings.Split(dest, "/") { if slices.Contains(strings.Split(dest, "/"), "..") {
if part == ".." { return "", errors.New("path contains '..', which is illegal")
return "", errors.New("path contains '..', which is illegal")
}
} }
// If a path is absolute, the creator of the TAR is doing something shady. // If a path is absolute, the creator of the TAR is doing something shady.

@ -18,6 +18,7 @@ package pusher
import ( import (
"fmt" "fmt"
"slices"
"helm.sh/helm/v4/pkg/cli" "helm.sh/helm/v4/pkg/cli"
"helm.sh/helm/v4/pkg/registry" "helm.sh/helm/v4/pkg/registry"
@ -86,12 +87,7 @@ type Provider struct {
// Provides returns true if the given scheme is supported by this Provider. // Provides returns true if the given scheme is supported by this Provider.
func (p Provider) Provides(scheme string) bool { func (p Provider) Provides(scheme string) bool {
for _, i := range p.Schemes { return slices.Contains(p.Schemes, scheme)
if i == scheme {
return true
}
}
return false
} }
// Providers is a collection of Provider objects. // Providers is a collection of Provider objects.

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"slices"
"strings" "strings"
"time" "time"
@ -45,12 +46,7 @@ func IsOCI(url string) bool {
// ContainsTag determines whether a tag is found in a provided list of tags // ContainsTag determines whether a tag is found in a provided list of tags
func ContainsTag(tags []string, tag string) bool { func ContainsTag(tags []string, tag string) bool {
for _, t := range tags { return slices.Contains(tags, tag)
if tag == t {
return true
}
}
return false
} }
func GetTagMatchingVersionOrConstraint(tags []string, versionString string) (string, error) { func GetTagMatchingVersionOrConstraint(tags []string, versionString string) (string, error) {

@ -22,6 +22,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"io" "io"
"slices"
rspb "helm.sh/helm/v4/pkg/release/v1" rspb "helm.sh/helm/v4/pkg/release/v1"
) )
@ -88,12 +89,7 @@ func decodeRelease(data string) (*rspb.Release, error) {
// Checks if label is system // Checks if label is system
func isSystemLabel(key string) bool { func isSystemLabel(key string) bool {
for _, v := range GetSystemLabels() { return slices.Contains(GetSystemLabels(), key)
if key == v {
return true
}
}
return false
} }
// Removes system labels from labels map // Removes system labels from labels map

Loading…
Cancel
Save