-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
144 lines (117 loc) · 2.73 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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"syscall"
)
var (
version = "unknown"
command []string
program = filepath.Base(os.Args[0])
)
func parseFlags() {
if len(os.Args) == 1 {
showUsage()
os.Exit(0)
}
command = os.Args[1:]
}
func showUsage() {
fmt.Fprintf(os.Stdout, "Usage: %s <command> [args]...\n", program)
fmt.Fprintf(os.Stdout, "Version: %s\n", version)
fmt.Fprintf(os.Stdout, "\n")
fmt.Fprintf(os.Stdout, " Chronic runs the <command> and hides the output unless the command returns a non-zero exit code.\n")
}
func tempFile(prefix string) *os.File {
var tempFile *os.File
var err error
if tempFile, err = ioutil.TempFile("", program+"-"+prefix); err != nil {
fatal(err)
}
return tempFile
}
func emitCommand() {
fmt.Fprintf(os.Stdout, "**** command ****\n")
fmt.Fprintf(os.Stdout, "%#q\n", command)
fmt.Fprintf(os.Stdout, "\n")
}
func emitOutput(name string, file io.ReadSeeker) {
shownHeader := false
if _, err := file.Seek(0, 0); err != nil {
fatal(err)
}
buff := bufio.NewScanner(file)
for buff.Scan() {
if !shownHeader {
fmt.Fprintf(os.Stdout, "**** %s ****\n", name)
shownHeader = true
}
fmt.Fprintf(os.Stdout, "%s: %s\n", name, buff.Text())
}
if shownHeader {
fmt.Fprintf(os.Stdout, "\n")
}
}
func fatal(err error) int {
fmt.Fprintf(os.Stdout, "[FATAL %s] %s\n", program, err)
if user, err := user.Current(); err == nil {
fmt.Fprintf(os.Stdout, "[FATAL %s] User: %q (%s)\n", program, user.Username, user.Uid)
}
fmt.Fprintf(os.Stdout, "[FATAL %s] $PATH: %s\n", program, os.Getenv("PATH"))
fmt.Fprintf(os.Stdout, "\n")
showUsage()
return -1
}
func run() int {
var stdout io.ReadCloser
var stderr io.ReadCloser
var err error
cmd := exec.Command(command[0], command[1:]...) /* #nosec G204 */
cmd.Stdin = os.Stdin
if stdout, err = cmd.StdoutPipe(); err != nil {
return fatal(err)
}
if stderr, err = cmd.StderrPipe(); err != nil {
return fatal(err)
}
if err = cmd.Start(); err != nil {
return fatal(err)
}
tmpOut := tempFile("stdout")
defer os.Remove(tmpOut.Name())
if _, err = io.Copy(tmpOut, stdout); err != nil {
return fatal(err)
}
tmpErr := tempFile("stderr")
defer os.Remove(tmpErr.Name())
if _, err = io.Copy(tmpErr, stderr); err != nil {
return fatal(err)
}
if err := cmd.Wait(); err != nil {
var exiterr *exec.ExitError
if errors.As(err, &exiterr) {
emitCommand()
emitOutput("stdout", tmpOut)
emitOutput("stderr", tmpErr)
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
ec := status.ExitStatus()
fmt.Fprintf(os.Stdout, "Exited with %d\n", ec)
return ec
}
} else {
return fatal(err)
}
}
return 0
}
func main() {
parseFlags()
os.Exit(run())
}