Skip to content

Commit

Permalink
Revert "feat: rework errorsList"
Browse files Browse the repository at this point in the history
This reverts commit 1a3cfa8.

Signed-off-by: Matthis Holleville <matthish29@gmail.com>
  • Loading branch information
matthisholleville committed May 2, 2023
1 parent 1a3cfa8 commit 8de1745
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 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)
}

errorsList := config.RunAnalysis()
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, errorsList)
output, err := config.PrintOutput(output)
if err != nil {
color.Red("Error: %v", err)
os.Exit(1)
Expand Down
17 changes: 6 additions & 11 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 []error
Errors []string
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() []error {
func (a *Analysis) RunAnalysis() {
activeFilters := viper.GetStringSlice("active_filters")

analyzerMap := analyzer.GetAnalyzerMap()
Expand All @@ -128,7 +128,6 @@ func (a *Analysis) RunAnalysis() []error {
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 @@ -142,7 +141,7 @@ func (a *Analysis) RunAnalysis() []error {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
errorsList = append(errorsList, 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 @@ -153,9 +152,7 @@ func (a *Analysis) RunAnalysis() []error {

}
wg.Wait()
return errorsList
}

semaphore = make(chan struct{}, a.MaxConcurrency)
// if the filters flag is specified
if len(a.Filters) != 0 {
Expand All @@ -170,7 +167,7 @@ func (a *Analysis) RunAnalysis() []error {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
errorsList = append(errorsList, 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 @@ -179,11 +176,10 @@ func (a *Analysis) RunAnalysis() []error {
<-semaphore
}(analyzer, filter)
} else {
errorsList = append(errorsList, 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 errorsList
}

var wg sync.WaitGroup
Expand All @@ -199,7 +195,7 @@ func (a *Analysis) RunAnalysis() []error {
results, err := analyzer.Analyze(analyzerConfig)
if err != nil {
mutex.Lock()
errorsList = append(errorsList, fmt.Errorf("[%s] %s", filter, err))
a.Errors = append(a.Errors, fmt.Sprintf("[%s] %s", filter, err))
mutex.Unlock()
}
mutex.Lock()
Expand All @@ -210,7 +206,6 @@ func (a *Analysis) RunAnalysis() []error {
}
}
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", []error{})
gotJson, err := analysis.PrintOutput("json")
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", []error{})
gotJson, err := analysis.PrintOutput("json")
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", []error{})
gotJson, err := analysis.PrintOutput("json")
if err != nil {
t.Error(err)
}
Expand Down
12 changes: 3 additions & 9 deletions pkg/analysis/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func getOutputFormats() []string {
return formats
}

func (a *Analysis) PrintOutput(format string, errorsList []error) ([]byte, error) {
a.Errors = errorsList
func (a *Analysis) PrintOutput(format string) ([]byte, error) {
outputFunc, ok := outputFormats[format]
if !ok {
return nil, fmt.Errorf("unsupported output format: %s. Available format %s", format, strings.Join(getOutputFormats(), ","))
Expand All @@ -42,15 +41,10 @@ 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: errorMessages,
Errors: a.Errors,
Status: status,
}
output, err := json.MarshalIndent(result, "", " ")
Expand All @@ -66,7 +60,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.Error())))
output.WriteString(fmt.Sprintf("- %s\n", color.YellowString(aerror)))
}
}
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
}

errorsList := config.RunAnalysis()
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, errorsList)
out, err := config.PrintOutput(s.Output)
if err != nil {
health.Failure++
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 8de1745

Please sign in to comment.