Skip to content

Commit ede16ce

Browse files
committed
Support multiple report instances
Previously, there was no provision for multiple report instances. Although the current tool design has no need for multiple instances, the code now has the versatility to allow for report demands that might arise in the future.
1 parent 35115a2 commit ede16ce

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

check/check.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ func RunChecks(project project.Type) {
3939
fmt.Printf("Running check %s: ", checkConfiguration.ID)
4040
}
4141
checkResult, checkOutput := checkConfiguration.CheckFunction()
42-
reportText := result.Record(project, checkConfiguration, checkResult, checkOutput)
42+
reportText := result.Report.Record(project, checkConfiguration, checkResult, checkOutput)
4343
if configuration.OutputFormat() == "text" {
4444
fmt.Print(reportText)
4545
}
4646
}
4747

4848
// Checks are finished for this project, so summarize its check results in the report.
49-
result.AddProjectSummaryReport(project)
49+
result.Report.AddProjectSummaryReport(project)
5050

5151
if configuration.OutputFormat() == "text" {
5252
// Print the project check results summary.
53-
fmt.Print(result.ProjectSummaryText(project))
53+
fmt.Print(result.Report.ProjectSummaryText(project))
5454
}
5555
}
5656

main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func main() {
1616
configuration.Initialize()
1717
// Must be called after configuration.Initialize()
18-
result.Initialize()
18+
result.Report.Initialize()
1919

2020
projects, err := project.FindProjects()
2121
if err != nil {
@@ -28,24 +28,24 @@ func main() {
2828
}
2929

3030
// All projects have been checked, so summarize their check results in the report.
31-
result.AddSummaryReport()
31+
result.Report.AddSummaryReport()
3232

3333
if configuration.OutputFormat() == "text" {
3434
if len(projects) > 1 {
3535
// There are multiple projects, print the summary of check results for all projects.
36-
fmt.Print(result.SummaryText())
36+
fmt.Print(result.Report.SummaryText())
3737
}
3838
} else {
3939
// Print the complete JSON formatted report.
40-
fmt.Println(result.JSONReport())
40+
fmt.Println(result.Report.JSONReport())
4141
}
4242

4343
if configuration.ReportFilePath() != nil {
4444
// Write report file.
45-
result.WriteReport()
45+
result.Report.WriteReport()
4646
}
4747

48-
if !result.Passed() {
48+
if !result.Report.Passed() {
4949
os.Exit(errorcodes.ErrGeneric)
5050
}
5151
}

result/result.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import (
1919
"github.com/arduino/go-paths-helper"
2020
)
2121

22-
type reportType struct {
22+
// Report is the global instance of the check results ReportType struct
23+
var Report ReportType
24+
25+
// ReportType is the type for the check results report
26+
type ReportType struct {
2327
Configuration toolConfigurationReportType `json:"configuration"`
2428
Projects []projectReportType `json:"projects"`
2529
Summary summaryReportType `json:"summary"`
@@ -63,10 +67,8 @@ type summaryReportType struct {
6367
ErrorCount int `json:"errorCount"`
6468
}
6569

66-
var report reportType
67-
6870
// Initialize adds the tool configuration data to the report.
69-
func Initialize() {
71+
func (report *ReportType) Initialize() {
7072
report.Configuration = toolConfigurationReportType{
7173
Paths: []*paths.Path{configuration.TargetPath()},
7274
ProjectType: configuration.SuperprojectTypeFilter().String(),
@@ -75,7 +77,7 @@ func Initialize() {
7577
}
7678

7779
// Record records the result of a check and returns a text summary for it.
78-
func Record(checkedProject project.Type, checkConfiguration checkconfigurations.Type, checkResult checkresult.Type, checkOutput string) string {
80+
func (report *ReportType) Record(checkedProject project.Type, checkConfiguration checkconfigurations.Type, checkResult checkresult.Type, checkOutput string) string {
7981
checkMessage := message(checkConfiguration.MessageTemplate, checkOutput)
8082

8183
checkLevel, err := checklevel.CheckLevel(checkConfiguration)
@@ -104,7 +106,7 @@ func Record(checkedProject project.Type, checkConfiguration checkconfigurations.
104106
Message: checkMessage,
105107
}
106108

107-
reportExists, projectReportIndex := getProjectReportIndex(checkedProject.Path)
109+
reportExists, projectReportIndex := report.getProjectReportIndex(checkedProject.Path)
108110
if !reportExists {
109111
// There is no existing report for this project.
110112
report.Projects = append(
@@ -130,8 +132,8 @@ func Record(checkedProject project.Type, checkConfiguration checkconfigurations.
130132
}
131133

132134
// AddProjectSummaryReport summarizes the results of all checks on the given project and adds it to the report.
133-
func AddProjectSummaryReport(checkedProject project.Type) {
134-
reportExists, projectReportIndex := getProjectReportIndex(checkedProject.Path)
135+
func (report *ReportType) AddProjectSummaryReport(checkedProject project.Type) {
136+
reportExists, projectReportIndex := report.getProjectReportIndex(checkedProject.Path)
135137
if !reportExists {
136138
panic(fmt.Sprintf("Unable to find report for %v when generating report summary", checkedProject.Path))
137139
}
@@ -158,8 +160,8 @@ func AddProjectSummaryReport(checkedProject project.Type) {
158160
}
159161

160162
// ProjectSummaryText returns a text summary of the check results for the given project.
161-
func ProjectSummaryText(checkedProject project.Type) string {
162-
reportExists, projectReportIndex := getProjectReportIndex(checkedProject.Path)
163+
func (report ReportType) ProjectSummaryText(checkedProject project.Type) string {
164+
reportExists, projectReportIndex := report.getProjectReportIndex(checkedProject.Path)
163165
if !reportExists {
164166
panic(fmt.Sprintf("Unable to find report for %v when generating report summary text", checkedProject.Path))
165167
}
@@ -169,7 +171,7 @@ func ProjectSummaryText(checkedProject project.Type) string {
169171
}
170172

171173
// AddSummaryReport summarizes the check results for all projects and adds it to the report.
172-
func AddSummaryReport() {
174+
func (report *ReportType) AddSummaryReport() {
173175
pass := true
174176
warningCount := 0
175177
errorCount := 0
@@ -189,16 +191,16 @@ func AddSummaryReport() {
189191
}
190192

191193
// SummaryText returns a text summary of the cumulative check results.
192-
func SummaryText() string {
194+
func (report ReportType) SummaryText() string {
193195
return fmt.Sprintf("Finished checking projects. Results:\nWarning count: %v\nError count: %v\nChecks passed: %v\n", report.Summary.WarningCount, report.Summary.ErrorCount, report.Summary.Pass)
194196
}
195197

196198
// Report returns a JSON formatted report of checks on all projects.
197-
func JSONReport() string {
198-
return string(jsonReportRaw())
199+
func (report ReportType) JSONReport() string {
200+
return string(report.jsonReportRaw())
199201
}
200202

201-
func jsonReportRaw() []byte {
203+
func (report ReportType) jsonReportRaw() []byte {
202204
reportJSON, err := json.MarshalIndent(report, "", " ")
203205
if err != nil {
204206
panic(fmt.Sprintf("Error while formatting checks report: %v", err))
@@ -208,21 +210,21 @@ func jsonReportRaw() []byte {
208210
}
209211

210212
// WriteReport writes a report for all projects to the specified file.
211-
func WriteReport() {
213+
func (report ReportType) WriteReport() {
212214
// Write report file
213-
err := configuration.ReportFilePath().WriteFile(jsonReportRaw())
215+
err := configuration.ReportFilePath().WriteFile(report.jsonReportRaw())
214216
if err != nil {
215217
feedback.Errorf("Error while writing report: %v", err)
216218
os.Exit(errorcodes.ErrGeneric)
217219
}
218220
}
219221

220222
// Passed returns whether the checks passed cumulatively.
221-
func Passed() bool {
223+
func (report ReportType) Passed() bool {
222224
return report.Summary.Pass
223225
}
224226

225-
func getProjectReportIndex(projectPath *paths.Path) (bool, int) {
227+
func (report ReportType) getProjectReportIndex(projectPath *paths.Path) (bool, int) {
226228
var index int
227229
var projectReport projectReportType
228230
for index, projectReport = range report.Projects {

0 commit comments

Comments
 (0)