Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace util.SliceContainsString with slices.Contains & make fmt #1041

Merged
merged 7 commits into from
Apr 19, 2024
7 changes: 3 additions & 4 deletions cmd/filters/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package filters

import (
"fmt"
"slices"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
Expand All @@ -40,10 +41,9 @@ var listCmd = &cobra.Command{
inactiveFilters := util.SliceDiff(availableFilters, activeFilters)
fmt.Print(color.YellowString("Active: \n"))
for _, filter := range activeFilters {

// if the filter is an integration, mark this differently
// 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))
} else {
// This strange bit of logic will loop through every integration via
Expand All @@ -60,13 +60,12 @@ var listCmd = &cobra.Command{
fmt.Print(color.YellowString("Unused: \n"))
for _, filter := range inactiveFilters {
// 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))
} else {
fmt.Printf("> %s\n", color.RedString(filter))
}
}
}

},
}
12 changes: 6 additions & 6 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ var ServeCmd = &cobra.Command{
envIsSet := backend != "" || password != "" || model != ""
if envIsSet {
aiProvider = &ai.AIProvider{
Name: backend,
Password: password,
Model: model,
BaseURL: baseURL,
Engine: engine,
Name: backend,
Password: password,
Model: model,
BaseURL: baseURL,
Engine: engine,
ProxyEndpoint: proxyEndpoint,
Temperature: temperature(),
Temperature: temperature(),
}

configAI.Providers = append(configAI.Providers, *aiProvider)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ai/azureopenai.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *AzureAIClient) Configure(config IAIConfig) error {
return azureModelMapping[model]

}

if proxyEndpoint != "" {
proxyUrl, err := url.Parse(proxyEndpoint)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *OpenAIClient) Configure(config IAIConfig) error {
Transport: transport,
}
}

client := openai.NewClientWithConfig(defaultConfig)
if client == nil {
return errors.New("error creating OpenAI client")
Expand Down
14 changes: 3 additions & 11 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ import (

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) {
if meta.OwnerReferences != nil {
for _, owner := range meta.OwnerReferences {
Expand Down Expand Up @@ -183,7 +174,8 @@ func GetCacheKey(provider string, language string, sEnc string) string {

func GetPodListByLabels(client k.Interface,
namespace string,
labels map[string]string) (*v1.PodList, error) {
labels map[string]string,
izturn marked this conversation as resolved.
Show resolved Hide resolved
) (*v1.PodList, error) {
pods, err := client.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
MatchLabels: labels,
Expand All @@ -207,7 +199,7 @@ func FileExists(path string) (bool, error) {
}

func EnsureDirExists(dir string) error {
err := os.MkdirAll(dir, 0755)
err := os.MkdirAll(dir, 0o755)
izturn marked this conversation as resolved.
Show resolved Hide resolved

if errors.Is(err, os.ErrExist) {
return nil
Expand Down
24 changes: 1 addition & 23 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,6 @@ import (
"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) {
ownerName := "test-name"
namespace := "test"
Expand Down Expand Up @@ -409,6 +386,7 @@ func TestGetPodListByLabels(t *testing.T) {
})
}
}

func TestFileExists(t *testing.T) {
tests := []struct {
filePath string
Expand Down
Loading