Skip to content

Commit

Permalink
feat: rework output format (#368)
Browse files Browse the repository at this point in the history
* feat: remove warning output

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: improves the error output to respect the format

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* feat: rework errorsList

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

* Revert "feat: rework errorsList"

This reverts commit 1a3cfa8.

Signed-off-by: Matthis Holleville <matthish29@gmail.com>

---------

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed May 3, 2023
1 parent e0200e7 commit 8b49f70
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 33 deletions.
7 changes: 1 addition & 6 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ var AnalyzeCmd = &cobra.Command{
os.Exit(1)
}

analysisErrors := config.RunAnalysis()
if len(analysisErrors) != 0 {
for _, err := range analysisErrors {
color.Red("Error: %s", err)
}
}
config.RunAnalysis()

if explain {
err := config.GetAIResults(output, anonymize)
Expand Down
9 changes: 0 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ limitations under the License.
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/cmd/analyze"
"github.com/k8sgpt-ai/k8sgpt/cmd/auth"
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
Expand Down Expand Up @@ -116,14 +114,7 @@ func performConfigMigrationIfNeeded() {
err = util.EnsureDirExists(configDir)
cobra.CheckErr(err)

if oldConfigExists && newConfigExists {
fmt.Fprintln(os.Stderr, color.RedString("Warning: Legacy config file at `%s` detected! This file will be ignored!", oldConfig))
return
}

if oldConfigExists && !newConfigExists {
fmt.Fprintln(os.Stderr, color.RedString("Performing config file migration from `%s` to `%s`", oldConfig, newConfig))

err = os.Rename(oldConfig, newConfig)
cobra.CheckErr(err)
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,23 @@ type Analysis struct {
Client *kubernetes.Client
AIClient ai.IAI
Results []common.Result
Errors []string
Namespace string
Cache cache.ICache
Explain bool
MaxConcurrency int
}

type AnalysisStatus string
type AnalysisErrors []string

const (
StateOK AnalysisStatus = "OK"
StateProblemDetected AnalysisStatus = "ProblemDetected"
)

type JsonOutput struct {
Errors AnalysisErrors `json:"errors"`
Status AnalysisStatus `json:"status"`
Problems int `json:"problems"`
Results []common.Result `json:"results"`
Expand Down Expand Up @@ -113,7 +116,7 @@ func NewAnalysis(backend string, language string, filters []string, namespace st
}, nil
}

func (a *Analysis) RunAnalysis() []error {
func (a *Analysis) RunAnalysis() {
activeFilters := viper.GetStringSlice("active_filters")

analyzerMap := analyzer.GetAnalyzerMap()
Expand All @@ -125,7 +128,6 @@ func (a *Analysis) RunAnalysis() []error {
AIClient: a.AIClient,
}

var errorList []error
semaphore := make(chan struct{}, a.MaxConcurrency)
// if there are no filters selected and no active_filters then run all of them
if len(a.Filters) == 0 && len(activeFilters) == 0 {
Expand All @@ -139,7 +141,7 @@ func (a *Analysis) RunAnalysis() []error {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
errorList = append(errorList, fmt.Errorf(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err)))
a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err)))
mutex.Unlock()
}
mutex.Lock()
Expand All @@ -150,7 +152,6 @@ func (a *Analysis) RunAnalysis() []error {

}
wg.Wait()
return errorList
}
semaphore = make(chan struct{}, a.MaxConcurrency)
// if the filters flag is specified
Expand All @@ -166,7 +167,7 @@ func (a *Analysis) RunAnalysis() []error {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
errorList = append(errorList, fmt.Errorf(fmt.Sprintf("[%s] %s", filter, err)))
a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("[%s] %s", filter, err)))
mutex.Unlock()
}
mutex.Lock()
Expand All @@ -175,11 +176,10 @@ func (a *Analysis) RunAnalysis() []error {
<-semaphore
}(analyzer, filter)
} else {
errorList = append(errorList, fmt.Errorf(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter)))
a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("\"%s\" filter does not exist. Please run k8sgpt filters list.", filter)))
}
}
wg.Wait()
return errorList
}

var wg sync.WaitGroup
Expand All @@ -195,7 +195,7 @@ func (a *Analysis) RunAnalysis() []error {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
errorList = append(errorList, fmt.Errorf("[%s] %s", filter, err))
a.Errors = append(a.Errors, fmt.Sprintf("[%s] %s", filter, err))
mutex.Unlock()
}
mutex.Lock()
Expand All @@ -206,7 +206,6 @@ func (a *Analysis) RunAnalysis() []error {
}
}
wg.Wait()
return errorList
}

func (a *Analysis) GetAIResults(output string, anonymize bool) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/analysis/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (a *Analysis) jsonOutput() ([]byte, error) {
result := JsonOutput{
Problems: problems,
Results: a.Results,
Errors: a.Errors,
Status: status,
}
output, err := json.MarshalIndent(result, "", " ")
Expand All @@ -55,6 +56,13 @@ func (a *Analysis) jsonOutput() ([]byte, error) {

func (a *Analysis) textOutput() ([]byte, error) {
var output strings.Builder
if len(a.Errors) != 0 {
output.WriteString("\n")
output.WriteString(color.YellowString("Warnings : \n"))
for _, aerror := range a.Errors {
output.WriteString(fmt.Sprintf("- %s\n", color.YellowString(aerror)))
}
}
output.WriteString("\n")
if len(a.Results) == 0 {
output.WriteString(color.GreenString("No problems detected\n"))
Expand Down
10 changes: 1 addition & 9 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,7 @@ func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
return
}

analysisErrors := config.RunAnalysis()
if analysisErrors != nil {
var errorMessage string
for _, err := range analysisErrors {
errorMessage += err.Error() + "\n"
}
http.Error(w, errorMessage, http.StatusInternalServerError)
health.Failure++
}
config.RunAnalysis()

if explain {
err := config.GetAIResults(s.Output, anonymize)
Expand Down

0 comments on commit 8b49f70

Please sign in to comment.