Skip to content

Commit b3e72ed

Browse files
committed
Add a function to control standard tool output based on --format setting
Previously, a conditional was needed for each print.
1 parent 7b5ac81 commit b3e72ed

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

check/check.go

+6-14
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ import (
2727
"github.com/arduino/arduino-check/project"
2828
"github.com/arduino/arduino-check/result"
2929
"github.com/arduino/arduino-check/result/feedback"
30-
"github.com/arduino/arduino-check/result/outputformat"
3130
"github.com/sirupsen/logrus"
3231
)
3332

3433
// RunChecks runs all checks for the given project and outputs the results.
3534
func RunChecks(project project.Type) {
36-
if configuration.OutputFormat() == outputformat.Text {
37-
fmt.Printf("Checking %s in %s\n", project.ProjectType, project.Path)
38-
}
35+
feedback.Printf("Checking %s in %s\n", project.ProjectType, project.Path)
3936

4037
checkdata.Initialize(project, configuration.SchemasPath())
4138

@@ -52,23 +49,18 @@ func RunChecks(project project.Type) {
5249
}
5350

5451
// Output will be printed after all checks are finished when configured for "json" output format
55-
if configuration.OutputFormat() == outputformat.Text {
56-
fmt.Printf("Running check %s: ", checkConfiguration.ID)
57-
}
52+
feedback.Printf("Running check %s: ", checkConfiguration.ID)
53+
5854
checkResult, checkOutput := checkConfiguration.CheckFunction()
5955
reportText := result.Results.Record(project, checkConfiguration, checkResult, checkOutput)
60-
if configuration.OutputFormat() == outputformat.Text {
61-
fmt.Print(reportText)
62-
}
56+
feedback.Print(reportText)
6357
}
6458

6559
// Checks are finished for this project, so summarize its check results in the report.
6660
result.Results.AddProjectSummary(project)
6761

68-
if configuration.OutputFormat() == outputformat.Text {
69-
// Print the project check results summary.
70-
fmt.Print(result.Results.ProjectSummaryText(project))
71-
}
62+
// Print the project check results summary.
63+
feedback.Print(result.Results.ProjectSummaryText(project))
7264
}
7365

7466
// shouldRun returns whether a given check should be run for the given project under the current tool configuration.

result/feedback/feedback.go

+14
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,23 @@ package feedback
1919
import (
2020
"fmt"
2121

22+
"github.com/arduino/arduino-check/configuration"
23+
"github.com/arduino/arduino-check/result/outputformat"
2224
"github.com/sirupsen/logrus"
2325
)
2426

27+
// Printf behaves like fmt.Printf but only prints when output format is set to `text`.
28+
func Printf(format string, v ...interface{}) {
29+
Print(fmt.Sprintf(format, v...))
30+
}
31+
32+
// Print behaves like fmt.Print but only prints when output format is set to `text`.
33+
func Print(message string) {
34+
if configuration.OutputFormat() == outputformat.Text {
35+
fmt.Printf(message)
36+
}
37+
}
38+
2539
// Errorf behaves like fmt.Printf but also logs the error.
2640
func Errorf(format string, v ...interface{}) {
2741
Error(fmt.Sprintf(format, v...))

0 commit comments

Comments
 (0)