-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
180 lines (151 loc) · 3.78 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
package main
import (
"bufio"
"errors"
"fmt"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"io"
"os"
"runtime"
"runtime/pprof"
)
var (
cpuProfileEnabled = false
colorOutputEnabled = os.Getenv("TERM") != "dumb" &&
(isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()))
outputStream = io.Writer(os.Stdout)
errorStream = os.Stderr
)
func init() {
// This application does not use any threads.
// Limiting GOMAXPROCS seems to have a positive effect on GC performance.
runtime.GOMAXPROCS(1)
}
func main() {
// There is some impedance mismatch between the stdlib flag package and my brain.
// Parse flags using custom code as there are so few of them.
var configFile string
setConfigFile := func(s string) {
if configFile == "" {
configFile = s
} else {
briefUsage()
exitFail()
}
}
configState := false
for _, arg := range os.Args[1:] {
if configState {
setConfigFile(arg)
configState = false
} else {
if len(arg) > 0 && arg[0] == '-' {
switch arg {
case "-color":
colorOutputEnabled = true
case "-config":
configState = true
case "-help", "--help" /* GNU concession */ :
detailedUsage()
exitSuccess()
default:
detailedUsage()
exitFail()
}
} else {
configDir, err := userConfigDir()
if err != nil {
fatalf("unable to find user config directory: %s\n", err)
}
setConfigFile(configDir + "/rainbow/" + arg + ".rainbow")
}
}
}
if configState || configFile == "" {
briefUsage()
exitFail()
}
if cpuProfileEnabled {
f, err := os.Create("rainbow-cpu.pprof")
if err == nil {
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
colorOutputEnabled = true
}
prog, err := loadProgram(configFile)
if err != nil {
fatalf("failed to read config: %s\n", err)
}
encoder := textEncoderDummy
if colorOutputEnabled {
outputStream = colorable.NewColorableStdout()
encoder = textEncoderANSI
}
bufferedOutputStream := bufio.NewWriter(outputStream)
line := newLine()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
// The line object and its state objects are reused beteween each line. The byte
// slice for the line content itself is uniquely allocated for each line as it's
// saved in a match history for match comparisons.
line.init(append([]byte(nil), scanner.Bytes()...))
if err = line.applyProgram(prog); err != nil {
fatalln(err.Error())
}
if err = line.output(bufferedOutputStream, encoder); err == nil {
err = bufferedOutputStream.Flush()
}
if err != nil {
fatalf("failed to output line: %s\n", err)
}
}
}
func detailedUsage() {
errorStream.Write([]byte(`Rainbow is a log file colorer that act as a stream processor. Match and action
rules are applied according to configuration to each line read from stdin,
outputting them to stdout.
`))
briefUsage()
}
func briefUsage() {
errorStream.Write([]byte(`Usage:
-help Show help
-color Force color for non-TTY output
-config FILE Use config FILE
CONFIG Use config from ~/.config/rainbow/CONFIG.rainbow
Example:
rainbow config < logfile
`))
}
// TODO(jb): Support for other platforms than Linux.
//
// This is currently Linix centric.
// There is the os.UserCacheDir() but I don't think that is the correct place to put user config files.
func userConfigDir() (string, error) {
var dir string
switch runtime.GOOS {
default:
dir = os.Getenv("HOME")
if dir == "" {
return "", errors.New("$HOME is not defined")
}
dir += "/.config"
}
return dir, nil
}
func fatalf(format string, a ...interface{}) {
fmt.Fprintf(errorStream, format, a...)
exitFail()
}
func fatalln(a ...interface{}) {
fmt.Fprintln(errorStream, a...)
exitFail()
}
func exitSuccess() {
os.Exit(0)
}
func exitFail() {
os.Exit(1)
}