This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (79 loc) · 1.62 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 (
"flag"
"fmt"
"github.com/fsnotify/fsnotify"
"log"
"os"
"os/exec"
"path"
)
const (
AppVersion = "0.0.1"
)
var (
configPath = flag.String("c", "autorun.config", "Profile path")
targetPath = flag.String("t", ".", "The target path")
version = flag.Bool("v", false, "autorun version")
showLog = flag.Bool("i", false, "show log")
)
var (
mainCmd *exec.Cmd
config *AutoRunConfig
done = make(chan bool)
)
func main() {
flag.Parse()
if *version {
fmt.Print(AppVersion)
os.Exit(0)
}
config = loadConfig(*configPath)
buildWatcher(*targetPath, done, func(event fsnotify.Event) {
Try(func() {
printLog("[file] ", event.Op, event.Name)
for _, e := range config.Exclude {
if ok, _ := path.Match(e, event.Name); ok {
printLog("[file ignore] ", event.Op, event.Name)
return
}
}
for _, i := range config.Include {
if ok, _ := path.Match(i, event.Name); ok {
printLog("[file Match] ", event.Op, event.Name)
restart()
return
}
}
}).Catch(func(i interface{}) {
printLog("[error] ", i)
})
}, func(err error) {
printLog("[error] ", err)
})
}
func printLog(v ...interface{}) {
if *showLog {
log.Println(v...)
}
}
func runCmd(command Command) *exec.Cmd {
cmd := exec.Command(command.Name, command.Args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Println("[error] ", err)
}
return cmd
}
func restart() {
if mainCmd != nil {
if err := mainCmd.Process.Kill(); err != nil {
log.Println("[error] ", err)
}
}
for _, cmd := range config.Build {
runCmd(cmd)
}
go runCmd(config.Run)
}