2017-08-29 19:47:29 +02:00
|
|
|
package formatter
|
2017-08-28 01:57:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2017-08-29 19:47:29 +02:00
|
|
|
"github.com/mgechev/revive/rule"
|
2017-08-28 01:57:16 +02:00
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
"github.com/ttacon/chalk"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
errorEmoji = ""
|
|
|
|
warningEmoji = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// CLIFormatter is an implementation of the Formatter interface
|
|
|
|
// which formats the errors to JSON.
|
|
|
|
type CLIFormatter struct {
|
|
|
|
Metadata FormatterMetadata
|
|
|
|
}
|
|
|
|
|
2017-08-29 19:47:29 +02:00
|
|
|
func formatFailure(failure rule.Failure) []string {
|
2017-08-28 01:57:16 +02:00
|
|
|
fString := chalk.Blue.Color(failure.Failure)
|
|
|
|
fTypeStr := string(failure.Type)
|
|
|
|
fType := chalk.Red.Color(fTypeStr)
|
|
|
|
lineColumn := failure.Position
|
|
|
|
pos := chalk.Dim.TextStyle(fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column))
|
2017-08-29 19:47:29 +02:00
|
|
|
if failure.Type == rule.FailureTypeWarning {
|
2017-08-28 01:57:16 +02:00
|
|
|
fType = chalk.Yellow.Color(fTypeStr)
|
|
|
|
}
|
|
|
|
return []string{failure.GetFilename(), pos, fType, fString}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format formats the failures gotten from the linter.
|
2017-08-29 19:47:29 +02:00
|
|
|
func (f *CLIFormatter) Format(failures []rule.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))
|
2017-08-29 19:47:29 +02:00
|
|
|
if f.Type == rule.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()
|
|
|
|
output += chalk.Dim.TextStyle(chalk.Underline.TextStyle(filename) + "\n")
|
|
|
|
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 {
|
|
|
|
suffix = chalk.Red.Color("\n ✖" + suffix)
|
|
|
|
} else if total > 0 && totalErrors == 0 {
|
|
|
|
suffix = chalk.Yellow.Color("\n ✖" + suffix)
|
|
|
|
} else {
|
|
|
|
suffix = chalk.Green.Color("\n" + suffix)
|
|
|
|
}
|
2017-08-28 01:57:16 +02:00
|
|
|
return output + suffix, nil
|
|
|
|
}
|