-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
90 lines (80 loc) · 1.78 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
package main
import (
"flag"
"fmt"
"os"
"github.com/fsnotify/fsnotify"
shellwords "github.com/mattn/go-shellwords"
)
var shellParser shellwords.Parser
func init() {
shellParser.ParseEnv = true
shellParser.ParseBacktick = true
}
func main() {
configFile := flag.String("c", "", "configuration file path")
flag.Parse()
mainLogger := NewLogFunc("main")
var (
config *Config
err error
)
if *configFile != "" {
config, err = NewConfig(*configFile)
if err != nil {
mainLogger(fmt.Sprintf("failed to create config: %s", err))
os.Exit(1)
}
} else {
config = NewDefaultConfig()
}
watcher, err := NewWatcher(config)
if err != nil {
mainLogger(fmt.Sprintf("failed to initialize watcher: %s", err))
os.Exit(1)
}
runner, err := NewRunner(config)
if err != nil {
mainLogger(fmt.Sprintf("failed to initialize runner: %s", err))
os.Exit(1)
}
builder, err := NewBuilder(config)
if err != nil {
mainLogger(fmt.Sprintf("failed to initialize builder: %s", err))
os.Exit(1)
}
if err := builder.Build(); err != nil {
mainLogger(fmt.Sprintf("failed to build: %s", err))
os.Exit(1)
}
err = runner.Serve()
if err != nil {
mainLogger(fmt.Sprintf("failed to start server: %s", err))
os.Exit(1)
}
defer runner.Stop()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.w.Events:
if !ok {
mainLogger("failed to catch an event")
}
mainLogger(fmt.Sprintf("event: %s", event))
if event.Op&fsnotify.Write == fsnotify.Write {
e := event.String()
mainLogger(fmt.Sprintf("file modified: %s", e))
if config.RestartProcess {
runner.Stop()
}
builder.Build()
runner.Serve()
}
case err := <-watcher.w.Errors:
mainLogger(fmt.Sprintf("error: %s", err))
}
}
}()
<-done
}