mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-08-25 10:58:34 +00:00
refactor: replace util.SliceContainsString with slices.Contains & make fmt (#1041)
* use std package's func instead Signed-off-by: gang.liu <gang.liu@daocloud.io> * refactor: replace util.SliceContainsString with slices.Contains & make fmt Signed-off-by: gang.liu <gang.liu@daocloud.io> --------- Signed-off-by: gang.liu <gang.liu@daocloud.io> Co-authored-by: Alex Jones <alexsimonjones@gmail.com>
This commit is contained in:
parent
693b23f1fc
commit
1ae4e75196
@ -15,6 +15,7 @@ package filters
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
|
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
|
||||||
@ -40,10 +41,9 @@ var listCmd = &cobra.Command{
|
|||||||
inactiveFilters := util.SliceDiff(availableFilters, activeFilters)
|
inactiveFilters := util.SliceDiff(availableFilters, activeFilters)
|
||||||
fmt.Print(color.YellowString("Active: \n"))
|
fmt.Print(color.YellowString("Active: \n"))
|
||||||
for _, filter := range activeFilters {
|
for _, filter := range activeFilters {
|
||||||
|
|
||||||
// if the filter is an integration, mark this differently
|
// if the filter is an integration, mark this differently
|
||||||
// but if the integration is inactive, remove
|
// but if the integration is inactive, remove
|
||||||
if util.SliceContainsString(integrationFilters, filter) {
|
if slices.Contains(integrationFilters, filter) {
|
||||||
fmt.Printf("> %s\n", color.BlueString("%s (integration)", filter))
|
fmt.Printf("> %s\n", color.BlueString("%s (integration)", filter))
|
||||||
} else {
|
} else {
|
||||||
// This strange bit of logic will loop through every integration via
|
// This strange bit of logic will loop through every integration via
|
||||||
@ -60,13 +60,12 @@ var listCmd = &cobra.Command{
|
|||||||
fmt.Print(color.YellowString("Unused: \n"))
|
fmt.Print(color.YellowString("Unused: \n"))
|
||||||
for _, filter := range inactiveFilters {
|
for _, filter := range inactiveFilters {
|
||||||
// if the filter is an integration, mark this differently
|
// if the filter is an integration, mark this differently
|
||||||
if util.SliceContainsString(integrationFilters, filter) {
|
if slices.Contains(integrationFilters, filter) {
|
||||||
fmt.Printf("> %s\n", color.BlueString("%s (integration)", filter))
|
fmt.Printf("> %s\n", color.BlueString("%s (integration)", filter))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("> %s\n", color.RedString(filter))
|
fmt.Printf("> %s\n", color.RedString(filter))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -33,15 +33,6 @@ import (
|
|||||||
|
|
||||||
var anonymizePattern = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;':\",./<>?")
|
var anonymizePattern = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;':\",./<>?")
|
||||||
|
|
||||||
func SliceContainsString(slice []string, s string) bool {
|
|
||||||
for _, item := range slice {
|
|
||||||
if item == s {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetParent(client *kubernetes.Client, meta metav1.ObjectMeta) (string, bool) {
|
func GetParent(client *kubernetes.Client, meta metav1.ObjectMeta) (string, bool) {
|
||||||
if meta.OwnerReferences != nil {
|
if meta.OwnerReferences != nil {
|
||||||
for _, owner := range meta.OwnerReferences {
|
for _, owner := range meta.OwnerReferences {
|
||||||
@ -183,7 +174,8 @@ func GetCacheKey(provider string, language string, sEnc string) string {
|
|||||||
|
|
||||||
func GetPodListByLabels(client k.Interface,
|
func GetPodListByLabels(client k.Interface,
|
||||||
namespace string,
|
namespace string,
|
||||||
labels map[string]string) (*v1.PodList, error) {
|
labels map[string]string,
|
||||||
|
) (*v1.PodList, error) {
|
||||||
pods, err := client.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{
|
pods, err := client.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{
|
||||||
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
|
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
|
||||||
MatchLabels: labels,
|
MatchLabels: labels,
|
||||||
@ -207,7 +199,7 @@ func FileExists(path string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EnsureDirExists(dir string) error {
|
func EnsureDirExists(dir string) error {
|
||||||
err := os.MkdirAll(dir, 0755)
|
err := os.MkdirAll(dir, 0o755)
|
||||||
|
|
||||||
if errors.Is(err, os.ErrExist) {
|
if errors.Is(err, os.ErrExist) {
|
||||||
return nil
|
return nil
|
||||||
|
@ -26,29 +26,6 @@ import (
|
|||||||
"k8s.io/client-go/kubernetes/fake"
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSliceContainsString(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
slice []string
|
|
||||||
s string
|
|
||||||
expected bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
expected: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
slice: []string{"temp", "value"},
|
|
||||||
s: "value",
|
|
||||||
expected: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
tt := tt
|
|
||||||
t.Run(tt.s, func(t *testing.T) {
|
|
||||||
require.Equal(t, tt.expected, SliceContainsString(tt.slice, tt.s))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetParent(t *testing.T) {
|
func TestGetParent(t *testing.T) {
|
||||||
ownerName := "test-name"
|
ownerName := "test-name"
|
||||||
namespace := "test"
|
namespace := "test"
|
||||||
@ -409,6 +386,7 @@ func TestGetPodListByLabels(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFileExists(t *testing.T) {
|
func TestFileExists(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
filePath string
|
filePath string
|
||||||
|
Loading…
Reference in New Issue
Block a user