-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
149 lines (123 loc) · 3.54 KB
/
run.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
145
146
147
148
149
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
path "path/filepath"
"runtime"
"strings"
)
var cmdRun = &Command{
UsageLine: "run [-m=./cmd/main.go] [-x=.go -x=.ini] [-a=../model] [-e=folderToExclude] [-tags=goBuildTags]",
Short: "run the app and start a Web server for development",
Long: `
Run command will supervise the file system of the go project using inotify,
it will recompile and restart the app after any modifications.
`,
}
// The main entrance of watcher
var mainPath string
// The extension list of the paths excluded from watching
var extensions strFlags
// The addon paths for wathing
var addonPaths strFlags
// The flags list of the paths excluded from watching
var excludedPaths strFlags
// Pass through to -tags arg of "go build"
var buildTags string
func init() {
cmdRun.Run = runApp
cmdRun.Flag.StringVar(&mainPath, "m", "", "main file path, default ./")
cmdRun.Flag.Var(&extensions, "x", "extension, default .go")
cmdRun.Flag.Var(&addonPaths, "a", "addon paths[].")
cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].")
cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)")
extensions = append(extensions, ".go")
excludedPaths = append(excludedPaths, "vendor")
}
var appname string
func runApp(cmd *Command, args []string) int {
fmt.Println("bat : " + version)
goversion, err := exec.Command("go", "version").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println("Go : " + strings.TrimSpace(string(goversion)))
fmt.Printf("Ext : %v\n", extensions)
fmt.Println("")
exit := make(chan bool)
crupath, _ := os.Getwd()
appname = path.Base(crupath)
ColorLog("[INFO] Uses '%s' as 'appname'\n", appname)
if strings.HasSuffix(appname, ".go") && isExist(path.Join(crupath, appname)) {
ColorLog("[WARN] The appname has conflic with crupath's file, do you want to build appname as %s\n", appname)
ColorLog("[INFO] Do you want to overwrite it? [yes|no]] ")
}
Debugf("current path:%s\n", crupath)
var paths []string
readAppDirectories(crupath, &paths)
// addon paths
for _, ap := range addonPaths {
readAppDirectories(ap, &paths)
}
// Because monitor files has some issues, we watch current directory
// and ignore non-go files.
gps := GetGOPATHs()
if len(gps) == 0 {
ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty")
os.Exit(2)
}
NewWatcher(paths, extensions)
Autobuild()
<-exit
runtime.Goexit()
return 0
}
func readAppDirectories(directory string, paths *[]string) {
fileInfos, err := ioutil.ReadDir(directory)
if err != nil {
return
}
useDirectory := false
for _, fileInfo := range fileInfos {
if strings.HasSuffix(fileInfo.Name(), "docs") {
continue
}
if isExcluded(path.Join(directory, fileInfo.Name())) {
continue
}
if fileInfo.IsDir() && fileInfo.Name()[0] != '.' {
readAppDirectories(directory+"/"+fileInfo.Name(), paths)
continue
}
if useDirectory {
continue
}
if checkExtension(fileInfo.Name(), extensions) {
*paths = append(*paths, directory)
useDirectory = true
}
}
}
// If a file is excluded
func isExcluded(filePath string) bool {
for _, p := range excludedPaths {
absP, err := path.Abs(p)
if err != nil {
ColorLog("[ERROR] Can not get absolute path of [ %s ]\n", p)
continue
}
absFilePath, err := path.Abs(filePath)
if err != nil {
ColorLog("[ERROR] Can not get absolute path of [ %s ]\n", filePath)
break
}
if strings.HasPrefix(absFilePath, absP) {
ColorLog("[INFO] Excluding from watching [ %s ]\n", filePath)
return true
}
}
return false
}