Skip to content

Commit

Permalink
feat: rework errorsList
Browse files Browse the repository at this point in the history
Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed May 2, 2023
1 parent a32d7d6 commit 1a3cfa8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var AnalyzeCmd = &cobra.Command{
os.Exit(1)
}

config.RunAnalysis()
errorsList := config.RunAnalysis()

if explain {
err := config.GetAIResults(output, anonymize)
Expand All @@ -62,7 +62,7 @@ var AnalyzeCmd = &cobra.Command{
}

// print results
output, err := config.PrintOutput(output)
output, err := config.PrintOutput(output, errorsList)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
Expand Down
17 changes: 11 additions & 6 deletions pkg/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Analysis struct {
Client *kubernetes.Client
AIClient ai.IAI
Results []common.Result
Errors []string
Errors []error
Namespace string
Cache cache.ICache
Explain bool
Expand Down Expand Up @@ -116,7 +116,7 @@ func NewAnalysis(backend string, language string, filters []string, namespace st
}, nil
}

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

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

var errorsList []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 @@ -141,7 +142,7 @@ func (a *Analysis) RunAnalysis() {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
a.Errors = append(a.Errors, fmt.Sprintf(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err)))
errorsList = append(errorsList, fmt.Errorf(fmt.Sprintf("[%s] %s", reflect.TypeOf(analyzer).Name(), err)))
mutex.Unlock()
}
mutex.Lock()
Expand All @@ -152,7 +153,9 @@ func (a *Analysis) RunAnalysis() {

}
wg.Wait()
return errorsList
}

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

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

func (a *Analysis) GetAIResults(output string, anonymize bool) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
Results: []common.Result{},
}

gotJson, err := analysis.PrintOutput("json")
gotJson, err := analysis.PrintOutput("json", []error{})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestAnalysis_ProblemJsonOutput(t *testing.T) {
},
}

gotJson, err := analysis.PrintOutput("json")
gotJson, err := analysis.PrintOutput("json", []error{})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
},
}

gotJson, err := analysis.PrintOutput("json")
gotJson, err := analysis.PrintOutput("json", []error{})
if err != nil {
t.Error(err)
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/analysis/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func getOutputFormats() []string {
return formats
}

func (a *Analysis) PrintOutput(format string) ([]byte, error) {
func (a *Analysis) PrintOutput(format string, errorsList []error) ([]byte, error) {
a.Errors = errorsList
outputFunc, ok := outputFormats[format]
if !ok {
return nil, fmt.Errorf("unsupported output format: %s. Available format %s", format, strings.Join(getOutputFormats(), ","))
Expand All @@ -41,10 +42,15 @@ func (a *Analysis) jsonOutput() ([]byte, error) {
status = StateOK
}

errorMessages := make([]string, len(a.Errors))
for i, err := range a.Errors {
errorMessages[i] = err.Error()
}

result := JsonOutput{
Problems: problems,
Results: a.Results,
Errors: a.Errors,
Errors: errorMessages,
Status: status,
}
output, err := json.MarshalIndent(result, "", " ")
Expand All @@ -60,7 +66,7 @@ func (a *Analysis) textOutput() ([]byte, error) {
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(fmt.Sprintf("- %s\n", color.YellowString(aerror.Error())))
}
}
output.WriteString("\n")
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
return
}

config.RunAnalysis()
errorsList := config.RunAnalysis()

if explain {
err := config.GetAIResults(s.Output, anonymize)
Expand All @@ -86,7 +86,7 @@ func (s *Config) analyzeHandler(w http.ResponseWriter, r *http.Request) {
}
}

out, err := config.PrintOutput(s.Output)
out, err := config.PrintOutput(s.Output, errorsList)
if err != nil {
health.Failure++
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 1a3cfa8

Please sign in to comment.