diff --git a/cmd/analyze/analyze.go b/cmd/analyze/analyze.go index 4d2785f6c6..986afbf520 100644 --- a/cmd/analyze/analyze.go +++ b/cmd/analyze/analyze.go @@ -2,7 +2,6 @@ package analyze import ( "context" - "encoding/base64" "encoding/json" "fmt" "os" @@ -97,7 +96,7 @@ var AnalyzeCmd = &cobra.Command{ for _, analysis := range *analysisResults { if explain { - parsedText, err := parseViaAI(ctx, aiClient, analysis.Error) + parsedText, err := analyzer.ParseViaAI(ctx, aiClient, analysis.Error) if err != nil { color.Red("Error: %v", err) continue @@ -129,44 +128,6 @@ var AnalyzeCmd = &cobra.Command{ }, } -func parseViaAI(ctx context.Context, aiClient ai.IAI, prompt []string) (string, error) { - // parse the text with the AI backend - - inputKey := strings.Join(prompt, " ") - // Check for cached data - sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey)) - // find in viper cache - if viper.IsSet(sEnc) { - // retrieve data from cache - response := viper.GetString(sEnc) - if response == "" { - color.Red("error retrieving cached data") - return "", nil - } - output, err := base64.StdEncoding.DecodeString(response) - if err != nil { - color.Red("error decoding cached data: %v", err) - return "", nil - } - return string(output), nil - } - - response, err := aiClient.GetCompletion(ctx, inputKey) - if err != nil { - color.Red("error getting completion: %v", err) - return "", nil - } - - if !viper.IsSet(sEnc) { - viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response))) - if err := viper.WriteConfig(); err != nil { - color.Red("error writing config: %v", err) - return "", nil - } - } - return response, nil -} - func init() { // array of strings flag diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index de26b1f82d..3501071699 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -2,9 +2,13 @@ package analyzer import ( "context" + "encoding/base64" + "strings" + "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/ai" "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" + "github.com/spf13/viper" ) func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error { @@ -20,13 +24,50 @@ func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI } err = AnalyzePersistentVolumeClaim(ctx, client, aiClient, explain, analysisResults) - if err != nil { + if err != nil { return err } - + err = AnalyzeEndpoints(ctx, client, aiClient, explain, analysisResults) if err != nil { return err } return nil } + +func ParseViaAI(ctx context.Context, aiClient ai.IAI, prompt []string) (string, error) { + // parse the text with the AI backend + inputKey := strings.Join(prompt, " ") + // Check for cached data + sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey)) + // find in viper cache + if viper.IsSet(sEnc) { + // retrieve data from cache + response := viper.GetString(sEnc) + if response == "" { + color.Red("error retrieving cached data") + return "", nil + } + output, err := base64.StdEncoding.DecodeString(response) + if err != nil { + color.Red("error decoding cached data: %v", err) + return "", nil + } + return string(output), nil + } + + response, err := aiClient.GetCompletion(ctx, inputKey) + if err != nil { + color.Red("error getting completion: %v", err) + return "", nil + } + + if !viper.IsSet(sEnc) { + viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response))) + if err := viper.WriteConfig(); err != nil { + color.Red("error writing config: %v", err) + return "", nil + } + } + return response, nil +}