-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
174 lines (154 loc) · 4.44 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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/spytheman/gostamp/terminal"
"io"
"log"
"os"
"os/exec"
"strings"
"sync"
"syscall"
)
type programSettings struct {
showVersion bool
useColor bool
useAbsolute bool
useCsv bool
showStart bool
showEnd bool
mergeErr bool
useElapsed bool
microSecond bool
nobuffering bool
}
var (
version string
cmdline = ""
settings programSettings
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "gostamp - Timestamp and colorize the stdout and stderr streams of CLI programs.\n")
fmt.Fprintf(os.Stderr, "Usage: %s [options] program [programoptions] \n", os.Args[0])
fmt.Fprintf(os.Stderr, " The options are:\n")
flag.PrintDefaults()
}
flag.BoolVar(&settings.showVersion, "version", false, "show the tool version")
flag.BoolVar(&settings.useColor, "color", true, "colorize the output")
flag.BoolVar(&settings.useCsv, "csv", false, "do not format the output at all, just show the time in ns, followed by ',' then the output")
flag.BoolVar(&settings.useAbsolute, "absolute", false, "use absolute timestamps")
flag.BoolVar(&settings.showStart, "start", true, "timestamp the start of the execution")
flag.BoolVar(&settings.showEnd, "end", true, "timestamp the end of the execution")
flag.BoolVar(&settings.mergeErr, "merge", false, "merge stderr to stdout. Useful for later filtering with grep.")
flag.BoolVar(&settings.useElapsed, "elapsed", false, "use timestamps, showing the elapsed time from the start of the program. Can not be used with -absolute")
flag.BoolVar(&settings.microSecond, "micro", false, "round timestamps to microseconds, instead of milliseconds. Can not be used with -absolute")
flag.BoolVar(&settings.nobuffering, "nobuf", false, "run the program with stdbuf -i0 -oL -eL, i.e. with *buffering off* for the std streams")
flag.Parse()
//fmt.Println(settings)
if settings.showVersion {
fmt.Println(version)
os.Exit(0)
}
if !settings.useColor {
terminal.TurnOffColor()
}
if settings.useCsv {
terminal.TurnOnCsv()
}
if settings.useAbsolute {
terminal.TurnOnAbsoluteTimestamps()
}
if settings.useElapsed {
terminal.TurnOnTimeRelativeToStart()
}
if settings.useAbsolute && settings.useElapsed {
fmt.Fprintf(os.Stderr, "-absolute and -elapsed can not be used together.\n")
os.Exit(-1)
}
if settings.useAbsolute && settings.microSecond {
fmt.Fprintf(os.Stderr, "-absolute and -micro can not be used together.\n")
os.Exit(-1)
}
if settings.mergeErr {
terminal.TurnOnCombineStderrAndStdout()
}
if settings.microSecond {
terminal.TurnOnMicrosecondTimestampResolution()
}
}
func main() {
if 0 == flag.NArg() {
flag.Usage()
os.Exit(1)
}
var cmd_args []string
if settings.nobuffering {
cmd_args = append(cmd_args, []string{"stdbuf", "-i0", "-oL", "-eL"}...)
}
cmd_args = append(cmd_args, flag.Args()...)
cmdline = strings.Join(cmd_args, " ")
//fmt.Printf("Running command: '%s' ...\n", cmdline)
command := exec.Command(cmd_args[0], cmd_args[1:]...)
commandIn, commandInErr := command.StdinPipe()
if commandInErr != nil {
log.Panic(commandInErr)
}
commandOut, commandOutErr := command.StdoutPipe()
if commandOutErr != nil {
log.Panic(commandOutErr)
}
commandErr, commandErrErr := command.StderrPipe()
if commandErrErr != nil {
log.Panic(commandErrErr)
}
scannerOut := bufio.NewScanner(commandOut)
scannerErr := bufio.NewScanner(commandErr)
var wg sync.WaitGroup
wg.Add(2)
go func() {
_, err := io.Copy(commandIn, os.Stdin)
if err != nil {
log.Fatal(err)
}
_ = commandIn.Close()
}()
go func() {
defer wg.Done()
for scannerErr.Scan() {
terminal.Err(scannerErr.Text())
}
}()
go func() {
defer wg.Done()
for scannerOut.Scan() {
terminal.Out(scannerOut.Text())
}
}()
if settings.showStart {
terminal.ResetPreviousTerminalLineTime()
terminal.Out("Start of '" + cmdline + "'")
}
// Setup is finished at this point. Run the command and process the results:
startErr := command.Start()
if startErr != nil {
terminal.Err("-->could not start, because of error: " + startErr.Error())
defer os.Exit(1)
}
waitError := command.Wait()
if waitError != nil {
terminal.Err("-->finished with error: " + waitError.Error())
if exitError, ok := waitError.(*exec.ExitError); ok {
if exitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
defer os.Exit(exitStatus.ExitStatus())
}
}
}
wg.Wait()
if settings.showEnd {
terminal.Out("End of '" + cmdline + "'")
}
terminal.Shutdown()
}