forked from kyoh86/richgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (77 loc) · 1.72 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
package main
import (
"io"
"os"
"os/exec"
"syscall"
"github.com/kyoh86/richgo/config"
"github.com/kyoh86/richgo/editor"
"github.com/kyoh86/richgo/editor/test"
)
const testFilterCmd = "testfilter"
const testCmd = "test"
type factoryFunc func() editor.Editor
var lps = map[string]factoryFunc{
"test": test.New,
}
func main() {
config.Load()
var cmd *exec.Cmd
var factory factoryFunc = editor.Parrot
var colorize bool
// without arguments
switch len(os.Args) {
case 0:
panic("no arguments")
case 1:
cmd = exec.Command("go")
default:
// This is a bit of a special case. Somebody is already
// running `go test` for us, and just wants us to prettify the
// output.
switch os.Args[1] {
case testFilterCmd:
colorize = true
cmd = exec.Command("cat", "-")
factory = test.New
case testCmd:
colorize = true
fallthrough
default:
cmd = exec.Command("go", os.Args[1:]...)
// select a wrapper with subcommand
if f, ok := lps[os.Args[1]]; ok {
factory = f
}
}
}
stderr := io.WriteCloser(os.Stderr)
stdout := io.WriteCloser(os.Stdout)
if colorize {
stderr = formatWriteCloser(os.Stderr, factory)
defer stderr.Close()
stdout = formatWriteCloser(os.Stdout, factory)
defer stdout.Close()
}
cmd.Stderr = stderr
cmd.Stdout = stdout
cmd.Stdin = os.Stdin
switch err := cmd.Run().(type) {
case nil:
// noop
default:
panic(err)
case *exec.ExitError:
if waitStatus, ok := err.Sys().(syscall.WaitStatus); ok {
defer os.Exit(waitStatus.ExitStatus())
} else {
panic(err)
}
}
}
func formatWriteCloser(wc io.WriteCloser, factory factoryFunc) io.WriteCloser {
if editor.Formattable(os.Stderr) {
return editor.Stream(wc, factory())
}
return editor.Stream(wc, editor.Parrot())
}