-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: export report handling into library code from cmd (#105)
- Loading branch information
Showing
2 changed files
with
42 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package protolock | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sort" | ||
) | ||
|
||
// HandleReport checks a report for warnigs and writes warnings to an io.Writer. | ||
// The returned int (an exit code) is 1 if warnings are encountered. | ||
func HandleReport(report *Report, w io.Writer, err error) (int, error) { | ||
if len(report.Warnings) > 0 { | ||
// sort the warnings so they are grouped by file location | ||
orderByPathAndMessage(report.Warnings) | ||
|
||
for _, warning := range report.Warnings { | ||
fmt.Fprintf( | ||
w, | ||
"CONFLICT: %s [%s]\n", | ||
warning.Message, warning.Filepath, | ||
) | ||
} | ||
return 1, err | ||
} | ||
|
||
return 0, err | ||
} | ||
|
||
func orderByPathAndMessage(warnings []Warning) { | ||
sort.Slice(warnings, func(i, j int) bool { | ||
if warnings[i].Filepath < warnings[j].Filepath { | ||
return true | ||
} | ||
if warnings[i].Filepath > warnings[j].Filepath { | ||
return false | ||
} | ||
return warnings[i].Message < warnings[j].Message | ||
}) | ||
} |