-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
127 lines (108 loc) · 2.67 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package lint
import (
"fmt"
"runtime"
"strings"
"github.com/fatih/color"
)
var (
red = color.New(color.FgRed).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()
)
type LinterMessages []LinterMessage
type LinterMessage struct {
isError bool // error/suggestion
caller string // linter func which created the message
sourceFile string // file containing path
path string // key path, e.g. key1.key2.setting
message string // mandatory message
description string // optional description, suggestion, information
}
func newError(sourceFile, path, message string, arg ...interface{}) LinterMessage {
return LinterMessage{
caller: getCaller(),
isError: true,
sourceFile: sourceFile,
path: path,
message: fmt.Sprintf(message, arg...),
description: "",
}
}
func newMessage(sourceFile, path, message string, arg ...interface{}) LinterMessage {
return LinterMessage{
caller: getCaller(),
isError: false,
sourceFile: sourceFile,
path: path,
message: fmt.Sprintf(message, arg...),
description: "",
}
}
func (lm LinterMessage) WithDescription(desc string, arg ...interface{}) LinterMessage {
lm.description = fmt.Sprintf(desc, arg...)
return lm
}
func (lm LinterMessage) IsError() bool {
return lm.isError
}
func (lm LinterMessage) String() string {
return lm.Message(false, true)
}
func (lm LinterMessage) Message(withCaller, withDescription bool) string {
outputColor := blue
if lm.isError {
outputColor = red
}
caller := ""
if lm.caller != "" && withCaller {
caller = fmt.Sprintf("[%s] ", lm.caller)
}
desc := ""
if lm.description != "" && withDescription {
desc = "\n" + strings.Repeat(" ", len(caller)) + lm.description
}
return fmt.Sprintf(
"%s%s: %s %s %s",
caller,
outputColor(lm.sourceFile),
outputColor("."+lm.path),
lm.message,
desc,
)
}
// LinterMessages implements sort.Interface
func (m LinterMessages) Len() int {
return len(m)
}
func (m LinterMessages) Less(i, j int) bool {
switch {
case m[i].isError == m[j].isError:
switch {
case m[i].sourceFile == m[j].sourceFile:
return m[i].path <= m[j].path
default:
return m[i].sourceFile <= m[j].sourceFile
}
case m[i].isError && !m[j].isError:
return true
case !m[i].isError && m[j].isError:
return false
default: // this should never happen
return false
}
}
func (m LinterMessage) Path() string {
return m.path
}
func (m LinterMessages) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func getCaller() string {
pc, _, _, ok := runtime.Caller(2)
details := runtime.FuncForPC(pc)
if ok && details != nil {
elem := strings.Split(details.Name(), ".")
return elem[len(elem)-1]
}
return ""
}