-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
61 lines (51 loc) · 1.14 KB
/
build.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
// +build ignore
package main
import (
"flag"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
log.SetOutput(os.Stdout)
log.SetFlags(0)
ensureGoPath()
flag.Parse()
if flag.NArg() == 0 {
log.Println("Usage: go run build.go build")
return
}
for _, cmd := range flag.Args() {
switch cmd {
case "build":
build("main", "./pkg/main/main.go")
}
}
}
func ensureGoPath() {
if os.Getenv("GOPATH") == "" {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
gopath := filepath.Clean(filepath.Join(cwd, "../"))
log.Println("GOPATH is", gopath)
os.Setenv("GOPATH", gopath)
}
}
func runPrint(cmd string, args ...string) {
log.Println(cmd, strings.Join(args, " "))
ecmd := exec.Command(cmd, args...)
ecmd.Stdout = os.Stdout
ecmd.Stderr = os.Stderr
err := ecmd.Run()
if err != nil {
log.Fatal(err)
}
}
func build(binaryName, pkg string) {
runPrint("go", "version")
runPrint("go", "build", "-o", "./bin/test-site", "-v", "./pkg/main")
}