-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
203 lines (168 loc) · 3.2 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type fnSprintFunc func(a ...interface{}) string // color's SprintFunc's signature
var (
c *exec.Cmd
stern bool
green = color.New(color.FgGreen).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
colors = []color.Attribute{
color.FgYellow,
color.FgBlue,
color.FgMagenta,
color.FgCyan,
color.FgWhite,
color.FgRed,
color.FgGreen,
}
rootCmd = &cobra.Command{
Use: "pretty",
Short: "JSON log prettifier wrapper tool",
Long: "JSON log prettifier wrapper tool.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
c = exec.Command(args[0])
}
if len(args) > 1 {
c = exec.Command(args[0], args[1:]...)
}
if c == nil {
log.Fatalln("no args")
}
outpipe, err := c.StdoutPipe()
if err != nil {
log.Fatalln(err)
}
errpipe, err := c.StderrPipe()
if err != nil {
log.Fatalln(err)
}
err = c.Start()
if err != nil {
log.Fatalln(err)
}
go func() {
outscan := bufio.NewScanner(outpipe)
print(green("[stdout]"), outscan)
}()
go func() {
errscan := bufio.NewScanner(errpipe)
print(red("[stderr]"), errscan)
}()
c.Wait()
},
}
)
func print(outpre string, scan *bufio.Scanner) {
var ci int
var cm map[string]fnSprintFunc
if stern {
cm = make(map[string]fnSprintFunc)
}
for {
chk := scan.Scan()
if !chk {
if scan.Err() != nil {
log.Fatalln(scan.Err())
}
}
b := scan.Bytes()
pre, s := prepare(b)
if pre != "" {
if stern {
fnclr, ok := cm[pre]
if ok {
pre = fnclr(pre)
} else {
fn := color.New(colors[ci]).SprintFunc()
cm[pre] = fn
pre = fn(pre)
ci += 1
if ci >= len(colors) {
ci = 0
}
}
}
log.Println(outpre, pre, s)
} else {
log.Println(outpre, s)
}
}
}
// Returns prefix (before the JSON part, if any), and the JSON string.
func prepare(b []byte) (string, string) {
var prefix string
s := string(b)
i1 := strings.Index(s, "[")
i2 := strings.Index(s, "{")
if i1 < 0 && i2 < 0 {
return prefix, s
}
if (i1 * i2) == 0 {
return prefix, pretty(s)
}
i := i1
if (i1 * i2) < 0 {
if i2 > i {
i = i2
}
} else {
if i2 < i {
i = i2
}
}
if i > 0 {
prefix = s[0:i]
}
return prefix, pretty(s[i:])
}
func pretty(v interface{}) string {
var out bytes.Buffer
var b []byte
_, ok := v.(string)
if !ok {
tmp, err := json.Marshal(v)
if err != nil {
return err.Error()
}
b = tmp
} else {
b = []byte(v.(string))
}
err := json.Indent(&out, b, "", " ")
if err != nil {
return v.(string)
}
return out.String()
}
func main() {
go func() {
s := make(chan os.Signal)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
sig := errors.Errorf("%s", <-s)
_ = sig
if c != nil {
err := c.Process.Signal(syscall.SIGTERM)
if err != nil {
log.Println("failed to terminate process, force kill...")
_ = c.Process.Signal(syscall.SIGKILL)
}
}
os.Exit(0)
}()
rootCmd.Flags().BoolVar(&stern, "stern", stern, "prefix color if using stern")
rootCmd.Execute()
}