2017-08-29 19:47:29 +02:00
|
|
|
package formatter
|
2017-08-28 01:57:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2017-11-20 04:44:42 +02:00
|
|
|
"github.com/fatih/color"
|
2018-01-22 04:04:41 +02:00
|
|
|
"github.com/mgechev/revive/linter"
|
2017-08-28 01:57:16 +02:00
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
errorEmoji = ""
|
|
|
|
warningEmoji = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// CLIFormatter is an implementation of the Formatter interface
|
|
|
|
// which formats the errors to JSON.
|
|
|
|
type CLIFormatter struct {
|
2018-01-22 04:04:41 +02:00
|
|
|
Metadata linter.FormatterMetadata
|
2017-08-28 01:57:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-22 04:04:41 +02:00
|
|
|
func formatFailure(failure linter.Failure) []string {
|
2017-11-20 04:44:42 +02:00
|
|
|
fString := color.BlueString(failure.Failure)
|
2017-08-28 01:57:16 +02:00
|
|
|
fTypeStr := string(failure.Type)
|
2017-11-20 04:44:42 +02:00
|
|
|
fType := color.RedString(fTypeStr)
|
2017-08-28 01:57:16 +02:00
|
|
|
lineColumn := failure.Position
|
2017-11-20 04:44:42 +02:00
|
|
|
pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
|
2018-01-22 04:04:41 +02:00
|
|
|
if failure.Type == linter.FailureTypeWarning {
|
2017-11-20 04:44:42 +02:00
|
|
|
fType = color.YellowString(fTypeStr)
|
2017-08-28 01:57:16 +02:00
|
|
|
}
|
|
|
|
return []string{failure.GetFilename(), pos, fType, fString}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format formats the failures gotten from the linter.
|
2018-01-22 04:04:41 +02:00
|
|
|
func (f *CLIFormatter) Format(failures []linter.Failure) (string, error) {
|
2017-08-28 01:57:16 +02:00
|
|
|
var result [][]string
|
|
|
|
var totalErrors = 0
|
|
|
|
for _, f := range failures {
|
|
|
|
result = append(result, formatFailure(f))
|
2018-01-22 04:04:41 +02:00
|
|
|
if f.Type == linter.FailureTypeError {
|
2017-08-28 01:57:16 +02:00
|
|
|
totalErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total := len(failures)
|
|
|
|
ps := "problems"
|
|
|
|
if total == 1 {
|
|
|
|
ps = "problem"
|
|
|
|
}
|
|
|
|
|
|
|
|
fileReport := make(map[string][][]string)
|
|
|
|
|
|
|
|
for _, row := range result {
|
|
|
|
if _, ok := fileReport[row[0]]; !ok {
|
|
|
|
fileReport[row[0]] = [][]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileReport[row[0]] = append(fileReport[row[0]], []string{row[1], row[2], row[3]})
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ""
|
|
|
|
for filename, val := range fileReport {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
table := tablewriter.NewWriter(buf)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetColumnSeparator("")
|
|
|
|
table.SetRowSeparator("")
|
|
|
|
table.SetAutoWrapText(false)
|
|
|
|
table.AppendBulk(val)
|
|
|
|
table.Render()
|
2017-11-20 04:44:42 +02:00
|
|
|
c := color.New(color.Underline)
|
|
|
|
output += c.SprintfFunc()(filename + "\n")
|
2017-08-28 01:57:16 +02:00
|
|
|
output += buf.String() + "\n"
|
|
|
|
}
|
|
|
|
|
2017-08-29 19:53:29 +02:00
|
|
|
suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors)
|
2017-08-28 01:57:16 +02:00
|
|
|
|
2017-08-29 19:53:29 +02:00
|
|
|
if total > 0 && totalErrors > 0 {
|
2017-11-20 04:44:42 +02:00
|
|
|
suffix = color.RedString("\n ✖" + suffix)
|
2017-08-29 19:53:29 +02:00
|
|
|
} else if total > 0 && totalErrors == 0 {
|
2017-11-20 04:44:42 +02:00
|
|
|
suffix = color.YellowString("\n ✖" + suffix)
|
2017-08-29 19:53:29 +02:00
|
|
|
} else {
|
2017-11-20 04:44:42 +02:00
|
|
|
suffix = color.GreenString("\n" + suffix)
|
2017-08-29 19:53:29 +02:00
|
|
|
}
|
2017-08-28 01:57:16 +02:00
|
|
|
return output + suffix, nil
|
|
|
|
}
|