-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathbuild.go
103 lines (93 loc) · 1.7 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
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
package main
import (
"log"
"flag"
"os"
"runtime"
"strings"
"os/exec"
"io"
)
const (
BinaryDir = "./bin/"
Binary = "easyproxy"
ConfDir = "./conf/"
Config = "default.json"
PKG = "./src"
)
func main() {
flag.Parse()
if flag.NArg() == 0 {
log.Println("please use go run build.go to build")
return
}
for _, cmd := range flag.Args() {
switch cmd {
case "build":
clean()
build()
case "clean":
clean()
default:
log.Printf("unknown command %s", cmd)
}
}
}
func execFile() string {
return BinaryDir + execName()
}
func confFile() string {
return ConfDir + Config
}
func execName() string {
name := Binary
if runtime.GOOS == "windows" {
name += ".exe"
}
return name
}
func build() {
log.Println("start building...")
args := []string{"build", "-ldflags", "-w -s"}
args = append(args, "-o", execFile())
args = append(args, PKG)
runCommand("go", args...)
copyConf()
}
func copyConf() {
DstDir := BinaryDir + ConfDir
os.Mkdir(DstDir, os.ModePerm)
src, err := os.Open(confFile())
if err != nil {
log.Println("cannot open file:", err)
return
}
defer src.Close()
dst, err := os.Create(DstDir + Config)
if err != nil {
log.Println("create conf file failed:", err)
return
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
log.Println("copy conf failed:", err)
}
}
func runCommand(cmd string, args ...string) {
log.Println(cmd, strings.Join(args, " "))
command := exec.Command(cmd, args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err := command.Run()
if err != nil {
log.Println(err)
}
}
func clean() {
log.Println("start cleaning...")
err := os.RemoveAll(BinaryDir)
if err != nil {
log.Println("clean files failed:", err)
}
}