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

Fix Go Report Card Issues #131

Merged
merged 15 commits into from
Apr 10, 2024
Prev Previous commit
Next Next commit
Fix gocyclo
HakanVardarr committed Mar 30, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit e9d7f48043b0c009c30953d5b0be4820d41b776d
85 changes: 52 additions & 33 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
@@ -105,52 +105,71 @@ func (c CLI) Run() (int, error) {
reports = append(reports, report)
}

err = c.printReports(reports)
if err != nil {
fmt.Println("failed to report:", err)
errorFound = true
}

if errorFound {
return 1, nil
} else {
return 0, nil
}
}

// printReports prints the reports based on the specified grouping and reporter type.
// It returns any error encountered during the printing process.
func (c CLI) printReports(reports []reporter.Report) error {
if Quiet {
return nil
}

reportGroup, err := c.groupReports(reports, GroupOutput[0])
if err != nil {
return err
}

// Group the output if the user specified a group by option
// Length is equal to one when empty as it contains an empty string
if len(GroupOutput) == 1 && GroupOutput[0] != "" {
reportGroup, err := GroupBySingle(reports, GroupOutput[0])
if err != nil {
return 1, fmt.Errorf("unable to group by single value: %v", err)
}
// Check reporter type to determine how to print
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintSingleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintSingleGroupStdout(reportGroup)
reporter.PrintSingleGroupJson(reportGroup.(map[string][]reporter.Report))
} else {
reporter.PrintSingleGroupStdout(reportGroup.(map[string][]reporter.Report))
}
} else if len(GroupOutput) == 2 {
reportGroup, err := GroupByDouble(reports, GroupOutput)
if err != nil {
return 1, fmt.Errorf("unable to group by double value: %v", err)
}
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintDoubleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintDoubleGroupStdout(reportGroup)
reporter.PrintDoubleGroupJson(reportGroup.(map[string]map[string][]reporter.Report))
} else {
reporter.PrintDoubleGroupStdout(reportGroup.(map[string]map[string][]reporter.Report))
}

} else if len(GroupOutput) == 3 {
reportGroup, err := GroupByTriple(reports, GroupOutput)
if err != nil {
return 1, fmt.Errorf("unable to group by triple value: %v", err)
}
if _, ok := c.Reporter.(reporter.JsonReporter); ok {
reporter.PrintTripleGroupJson(reportGroup)
} else if !Quiet {
reporter.PrintTripleGroupStdout(reportGroup)
reporter.PrintTripleGroupJson(reportGroup.(map[string]map[string]map[string][]reporter.Report))
} else {
reporter.PrintTripleGroupStdout(reportGroup.(map[string]map[string]map[string][]reporter.Report))
}
} else {
if !Quiet {
err = c.Reporter.Print(reports)
}
if err != nil {
fmt.Println("failed to report:", err)
errorFound = true
}
return c.Reporter.Print(reports)
}
if errorFound {
return 1, nil
} else {
return 0, nil

return nil

}

// groupReports groups the given reports based on the specified grouping option.
// It returns the grouped reports and any error encountered during the grouping process.
func (c CLI) groupReports(reports []reporter.Report, group string) (interface{}, error) {
switch len(GroupOutput) {
case 1:
return GroupBySingle(reports, group)
case 2:
return GroupByDouble(reports, GroupOutput)
case 3:
return GroupByTriple(reports, GroupOutput)
default:
return nil, fmt.Errorf("Invalid number of group outputs: %d", len(GroupOutput))
}
}