-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
62 lines (49 loc) · 885 Bytes
/
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
package main
import (
"bufio"
"fmt"
"os"
cs2log "github.com/janstuemmel/cs2-log"
)
// Usage:
//
// From file:
// go run main.go example.log
//
// From STDIN:
// cat example.log | go run main.go
//
// To File:
// go run main.go > out.txt
//
// Omit errors:
// go run main.go 2>/dev/null
func main() {
var file *os.File
var err error
if len(os.Args) < 2 {
file = os.Stdin
} else {
file, err = os.Open(os.Args[1])
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
r := bufio.NewReader(file)
// read first line
l, _, err := r.ReadLine()
for err == nil {
// parse
m, errParse := cs2log.Parse(string(l))
if errParse != nil {
// print parse errors to stderr
fmt.Fprintf(os.Stderr, "ERROR: %s", cs2log.ToJSON(m))
} else {
// print to stdout
fmt.Fprintf(os.Stdout, "%s", cs2log.ToJSON(m))
}
// next line
l, _, err = r.ReadLine()
}
}