-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (177 loc) · 3.75 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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/mitchellh/go-homedir"
)
var defaultDateTimeLayout = time.RFC3339
var defaultSlowThreshold int64 = 500
var showTS = ""
var showElapsed = true
var showDelta = true
var slow int64 = 0
// getArgs from CLI or config file
func getArgs() []string {
args := os.Args[1:]
if len(args) != 0 {
return args
}
// no cli args given, look for config file
dir, err := homedir.Dir()
if err != nil {
return args
}
rc := filepath.Join(dir, ".elapsedrc")
_, err = os.Stat(rc)
if err != nil {
return args
}
b, err := os.ReadFile(rc)
if err != nil {
return args
}
return strings.Split(strings.TrimSpace(string(b)), " ")
}
func parseFlags() {
st := flag.Bool("datetime", false, "Show date/time stamp when message was received")
stf := flag.String("format", "", "Date/time format (default: "+defaultDateTimeLayout+")")
se := flag.Bool("no-elapsed", false, "Do not print the absolute elapsed time")
sd := flag.Bool("no-delta", false, "Do not print the delta elapsed time")
sl := flag.Bool("slow", false, "Show only slow deltas (over a certain threshold)")
slm := flag.Int64("slow-ms", defaultSlowThreshold, "Slow delta threshold in ms")
flag.CommandLine.Parse(getArgs())
if se != nil && *se {
showElapsed = false
}
if sd != nil && *sd {
showDelta = false
}
if st != nil && *st {
if stf != nil && *stf != "" {
showTS = *stf
} else {
showTS = defaultDateTimeLayout
}
}
if sl != nil && *sl {
slow = defaultSlowThreshold
}
if slm != nil && *slm != defaultSlowThreshold {
slow = *slm
}
}
func run(r io.Reader) {
f := "["
if showTS != "" {
f += "%s"
if showElapsed || showDelta {
f += " | "
}
}
if showElapsed {
f += "%-7s"
}
if showDelta {
if showElapsed {
f += " "
}
f += "%-8s"
}
f += "] %s"
reader := bufio.NewReader(r)
start := time.Now()
last := time.Now()
for {
line, err := reader.ReadString('\n')
if err != nil {
return
}
var args []interface{}
if showTS != "" {
// add current date/time stamp
args = append(args, time.Now().Format(showTS))
}
if showElapsed {
// absolute elapsed time
elapsed := time.Since(start).Round(1 * time.Microsecond).Milliseconds()
elapsedS := fmt.Sprintf("%dms", elapsed)
args = append(args, elapsedS)
}
if showDelta {
// delta between last line
delta := time.Since(last).Round(1 * time.Microsecond).Milliseconds()
if delta >= slow {
deltaS := fmt.Sprintf("+%dms", delta)
args = append(args, deltaS)
} else {
args = append(args, "")
}
}
args = append(args, line)
// "[%-7s +%-7s] %s"
fmt.Printf(f, args...)
if showDelta {
// update last line ts
last = time.Now()
}
}
}
func getReader() (io.Reader, []string) {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
// look for cmd
args := os.Args[1:]
if len(args) != 0 {
for i, arg := range args {
if arg == "--" && i+1 < len(args) && args[i+1] != "" {
return nil, args[i+1:]
}
}
}
fmt.Println("error: stdin not connected to a pipe")
os.Exit(1)
}
return os.Stdin, nil
}
func runCmd(args []string) {
cmd := exec.Command(args[0], args[1:]...)
r, w := io.Pipe()
cmd.Stdout = w
cmd.Stderr = w
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
err := cmd.Start()
if err != nil {
fmt.Println("error running command: ", err)
}
go run(r)
err = cmd.Wait()
if err != nil {
fmt.Println("error waiting for command: ", err)
}
os.Exit(cmd.ProcessState.ExitCode())
}()
wg.Wait()
}
func main() {
parseFlags()
if !(showElapsed || showDelta) {
fmt.Println("error: must enable either elapsed or delta")
os.Exit(1)
}
r, args := getReader()
if args != nil {
runCmd(args)
} else {
run(r)
}
}