-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
67 lines (59 loc) · 1.33 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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/fatih/color"
"github.com/psykhi/uno/pkg/processor"
"log"
"os"
"strings"
)
func main() {
printAll := flag.Bool("all", false, "Print all lines and highlight new lines in red (if the terminal supports it)")
maxDiffRatio := flag.Float64("d", 0.2, "The maximum difference ratio between the input line and the other lines seen (between 0 and 1)")
patterns := flag.Bool("p", false, "Show log patterns")
flag.Parse()
if *maxDiffRatio < 0 || *maxDiffRatio > 1 {
log.Fatalf("Distance ratio must be between 0 and 1")
}
// Read from stdin or from file
f := os.Stdin
if len(os.Args) >= 2 {
var err error
lastArg := os.Args[len(os.Args)-1]
if !strings.HasPrefix(lastArg, "-") {
f, err = os.Open(lastArg)
if err != nil {
panic(err)
}
defer f.Close()
}
}
scanner := bufio.NewScanner(f)
p := processor.NewProcessor(*maxDiffRatio)
l := processor.Line{}
for scanner.Scan() {
l.Input = scanner.Bytes()
l.IsNew = false
l = p.Process(l)
toPrint := l.Input
if *patterns {
toPrint = []byte(strings.Join(l.Tokens, ""))
}
if l.IsNew {
if *printAll {
color.Red("%s", toPrint)
} else {
fmt.Printf("%s\n", toPrint)
}
continue
}
if *printAll {
fmt.Printf("%s\n", toPrint)
}
}
if scanner.Err() != nil {
panic(scanner.Err())
}
}