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
// supported by helm. If so, mark the hook as one should be deleted.
func hookHasDeletePolicy(h *release.Hook, policy release.HookDeletePolicy) bool {
for _, v := range h.DeletePolicies {
if policy == v {
return true
}
}
return false
return slices.Contains(h.DeletePolicies, policy)
}
// outputLogsByPolicy outputs a pods logs if the hook policy instructs it to

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

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

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

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

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

@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"net/http"
"slices"
"time"
"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.
func (p Provider) Provides(scheme string) bool {
for _, i := range p.Schemes {
if i == scheme {
return true
}
}
return false
return slices.Contains(p.Schemes, scheme)
}
// Providers is a collection of Provider objects.

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

@ -27,6 +27,7 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"strings"
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
// is not a good practice.
for _, part := range strings.Split(dest, "/") {
if part == ".." {
return "", errors.New("path contains '..', which is illegal")
}
if slices.Contains(strings.Split(dest, "/"), "..") {
return "", errors.New("path contains '..', which is illegal")
}
// If a path is absolute, the creator of the TAR is doing something shady.

@ -18,6 +18,7 @@ package pusher
import (
"fmt"
"slices"
"helm.sh/helm/v4/pkg/cli"
"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.
func (p Provider) Provides(scheme string) bool {
for _, i := range p.Schemes {
if i == scheme {
return true
}
}
return false
return slices.Contains(p.Schemes, scheme)
}
// Providers is a collection of Provider objects.

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

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

Loading…
Cancel
Save