-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
286 lines (258 loc) · 6.99 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
termcolor "github.com/fatih/color"
)
// Expected log line output
// [LOG] 0:00.003 INFO a.package body
// TODO: Perhaps error deserves a better coloring
var (
errColorP = termcolor.New(termcolor.FgGreen).Add(termcolor.Underline).PrintlnFunc()
logSqBracketSP = termcolor.New(termcolor.FgWhite, termcolor.Bold).SprintFunc()
logSP = termcolor.New(termcolor.FgYellow, termcolor.Bold).SprintFunc()
bracketSP = termcolor.New(termcolor.FgBlue).SprintFunc()
quoteSP = termcolor.New(termcolor.FgBlue, termcolor.Bold, termcolor.Italic).SprintFunc()
kvNumSP = termcolor.New(termcolor.FgYellow, termcolor.Bold).SprintFunc()
kvSepSP = termcolor.New(termcolor.FgWhite, termcolor.Bold, termcolor.Italic).SprintFunc()
strSP = termcolor.New(termcolor.FgWhite, termcolor.Bold).SprintFunc()
dateSP = termcolor.New(termcolor.FgRed, termcolor.Italic).SprintFunc()
packSP = termcolor.New(termcolor.FgWhite, termcolor.Italic, termcolor.Bold).SprintFunc()
escapeSP = termcolor.New(termcolor.FgCyan, termcolor.Bold).SprintFunc()
escapedSP = termcolor.New(termcolor.FgMagenta, termcolor.Bold).SprintFunc()
logHeading = fmt.Sprintf("%s%s%s", logSqBracketSP("["), logSP("LOG"), logSqBracketSP("]"))
debugL = termcolor.CyanString("DEBUG")
errorL = termcolor.RedString("ERROR")
infoL = termcolor.GreenString("INFO")
warnL = termcolor.MagentaString("WARN")
)
type nologArgs struct {
outToFile bool
outFileName string
color bool
verbose bool
gocheck string
args []string
}
var nlArgs nologArgs
func init() {
flag.BoolVar(&nlArgs.outToFile, "f", false, "setting this flag will output the logs to a file.")
flag.StringVar(&nlArgs.outFileName, "name", "tests.log", "this is an alternative file name for the ouput.")
flag.BoolVar(&nlArgs.color, "c", false, "setting this flag will color the output logs.")
flag.BoolVar(&nlArgs.verbose, "v", false, "setting this flag will use -test.v=true on the test run.")
flag.StringVar(&nlArgs.gocheck, "filter", "", "this will be used to filter tests with -gocheck.f (requires gocheck).")
flag.Parse()
nlArgs.args = flag.Args()
}
func main() {
args := nlArgs.args
var (
f *os.File
err error
)
if nlArgs.outToFile {
f, err = os.OpenFile(nlArgs.outFileName, os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file %q: %v", nlArgs.outFileName, err)
os.Exit(1)
}
defer f.Close()
}
testFlags := []string{}
if nlArgs.verbose || nlArgs.gocheck != "" {
testFlags = append(testFlags, "-test.v=true")
}
if nlArgs.gocheck != "" {
testFlags = append(testFlags, fmt.Sprintf("-gocheck.f=%s", nlArgs.gocheck))
}
args = append([]string{"test"}, args...)
args = append(args, testFlags...)
if len(args) == 1 {
args = append(args, "./...")
}
cmd := exec.Command("go", args...)
out, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting StdoutPipe from command: %v", err)
os.Exit(1)
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting StderrPipe from command: %v", err)
os.Exit(1)
}
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Error running command: %v", err)
os.Exit(1)
}
wg := &sync.WaitGroup{}
wg.Add(2)
var colorOut, colorErr chan string
if nlArgs.color {
colorOut = make(chan string)
colorErr = make(chan string)
wg.Add(2)
go colorizeOut(colorOut, wg)
go colorizeErr(colorErr, wg)
}
go printOut(f, out, colorOut, wg)
go printErr(f, stderr, colorErr, wg)
wg.Wait()
}
func tokenize(s string) string {
tokenized := []string{" "}
bracketCount := 0
inStr := false
escaped := false
for k, _ := range s {
// TODO (perrito666) make this more rune friendly
c := s[k : k+1] // This will break a lot with unicode chars
if escaped {
escaped = false
tokenized = append(tokenized, escapedSP(c))
continue
}
switch c {
case "{":
bracketCount += 1
tokenized = append(tokenized, bracketSP(c))
case "}":
bracketCount -= 1
tokenized = append(tokenized, bracketSP(c))
case "\\":
escaped = true
tokenized = append(tokenized, escapeSP(c))
case "\"":
inStr = !inStr
tokenized = append(tokenized, quoteSP(c))
case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0":
// I know, regexes would be better right? NO
switch {
case bracketCount > 0 && !inStr:
tokenized = append(tokenized, kvNumSP(c))
case inStr:
tokenized = append(tokenized, strSP(c))
default:
tokenized = append(tokenized, c)
}
case ":":
if bracketCount > 0 {
tokenized = append(tokenized, kvSepSP(c))
} else {
tokenized = append(tokenized, c)
}
default:
if inStr {
tokenized = append(tokenized, strSP(c))
} else {
tokenized = append(tokenized, c)
}
}
}
return strings.Join(tokenized, "")
}
func nextWord(s string) (string, string) {
s = strings.TrimLeft(s, " ")
words := strings.SplitN(s, " ", 2)
return words[0], words[1]
}
func extractDateInto(s, out string) (string, string) {
date, s := nextWord(s)
out = fmt.Sprintf("%s %s", out, dateSP(date))
return out, s
}
func extractLogLevel(s, out string) (string, string) {
logLevel, s := nextWord(s)
switch {
case strings.HasPrefix(logLevel, "DEBUG"):
logLevel = debugL
case strings.HasPrefix(logLevel, "ERROR"):
logLevel = errorL
case strings.HasPrefix(logLevel, "INFO"):
logLevel = infoL
case strings.HasPrefix(logLevel, "WARN"):
logLevel = warnL
}
out = fmt.Sprintf("%s %s", out, logLevel)
return out, s
}
func extractPackage(s, out string) (string, string) {
pack, s := nextWord(s)
out = fmt.Sprintf("%s %s", out, packSP(pack))
return out, s
}
func colorizeOut(c chan string, wg *sync.WaitGroup) {
for {
s, ok := <-c
if !ok {
wg.Done()
return
}
var out string
if strings.HasPrefix(s, "[LOG]") {
out = logHeading
_, s = nextWord(s)
out, s = extractDateInto(s, out)
out, s = extractLogLevel(s, out)
out, s = extractPackage(s, out)
}
out += tokenize(s)
fmt.Println(out)
}
}
func colorizeErr(c chan string, wg *sync.WaitGroup) {
for {
s, ok := <-c
if !ok {
wg.Done()
return
}
errColorP(s)
}
}
func printOut(f *os.File, r io.Reader, c chan string, wg *sync.WaitGroup) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := string(scanner.Bytes())
if f != nil {
fmt.Fprintln(f, s)
}
if !strings.HasPrefix(s, "[LOG]") && c == nil {
fmt.Println(s)
}
if c != nil {
c <- s
}
}
if scanner.Err() != nil {
fmt.Fprintf(os.Stderr, "Error reading output of go test: %v", scanner.Err())
os.Exit(1)
}
close(c)
wg.Done()
}
func printErr(f *os.File, r io.Reader, c chan string, wg *sync.WaitGroup) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := string(scanner.Bytes())
if f != nil {
fmt.Fprintln(f, s)
}
fmt.Fprintln(os.Stderr, s)
if c != nil {
c <- s
}
}
if scanner.Err() != nil {
fmt.Fprintf(os.Stderr, "Error reading output of go test: %v", scanner.Err())
os.Exit(1)
}
close(c)
wg.Done()
}