-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
95 lines (78 loc) · 2.04 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
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"os/exec"
"path"
"strings"
)
const (
testSuffix = "_test.go"
)
var (
prefix = flag.String("prefix", "flymake_", "The prefix for generated Flymake artifacts.")
debug = flag.Bool("debug", false, "Enable extra diagnostic output to determine why errors are occurring.")
testArguments = []string{"test", "-c"}
buildArguments = []string{"build", "-o", "/dev/null"}
)
func main() {
flag.Parse()
goflymakeArguments := flag.Args()
if len(goflymakeArguments) != 1 {
log.Fatalf("%s %ssome_file.go", path.Base(os.Args[0]), *prefix)
}
file := goflymakeArguments[0]
base := path.Base(file)
if !strings.HasPrefix(base, *prefix) {
log.Fatalf("%s lacks the appropriate filename prefix %s", base, *prefix)
}
orig := base[len(*prefix):]
isTest := false
var goArguments []string
if strings.HasSuffix(orig, testSuffix) {
isTest = true
// shame there is no '-o' option
goArguments = append(goArguments, testArguments...)
} else {
goArguments = append(goArguments, buildArguments...)
}
sdir := path.Dir(file)
pkg, err := build.ImportDir(sdir, build.AllowBinary)
if err != nil {
goArguments = append(goArguments, file)
} else {
var files []string
files = append(files, pkg.GoFiles...)
files = append(files, pkg.CgoFiles...)
if isTest {
files = append(files, pkg.TestGoFiles...)
files = append(files, pkg.XTestGoFiles...)
}
for _, f := range files {
if f == orig {
continue
}
goArguments = append(goArguments, f)
}
}
cmd := exec.Command("go", goArguments...)
out, err := cmd.CombinedOutput()
fmt.Print(string(out))
if err != nil {
if *debug {
banner := strings.Repeat("*", 80)
log.Println(banner)
log.Println("Encountered a problem:", err)
log.Println("goflymake ARGUMENTS:", os.Args)
log.Println("Go ARGUMENTS:", goArguments)
log.Println("ENVIRONMENT VARIABLES")
log.Println("PATH:", os.Getenv("PATH"))
log.Println("GOPATH", os.Getenv("GOPATH"))
log.Println("GOROOT", os.Getenv("GOROOT"))
log.Println(banner)
}
}
}