forked from acoshift/goreload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (149 loc) · 3.67 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"errors"
"fmt"
"github.com/acoshift/goreload/internal"
shellwords "github.com/mattn/go-shellwords"
"gopkg.in/urfave/cli.v1"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
)
var (
startTime = time.Now()
logger = log.New(os.Stdout, "[goreload] ", 0)
colorGreen = string([]byte{27, 91, 57, 55, 59, 51, 50, 59, 49, 109})
colorRed = string([]byte{27, 91, 57, 55, 59, 51, 49, 59, 49, 109})
colorReset = string([]byte{27, 91, 48, 109})
)
func main() {
app := cli.NewApp()
app.Name = "goreload"
app.Usage = "A live reload utility for Go web applications."
app.Action = mainAction
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "bin,b",
Value: ".goreload",
Usage: "name of generated binary file",
},
cli.StringFlag{
Name: "path,t",
Value: ".",
Usage: "Path to watch files from",
},
cli.StringFlag{
Name: "build,d",
Value: "",
Usage: "Path to build files from (defaults to same value as --path)",
},
cli.StringSliceFlag{
Name: "excludeDir,x",
Value: &cli.StringSlice{},
Usage: "Relative directories to exclude",
},
cli.BoolFlag{
Name: "all",
Usage: "reloads whenever any file changes, as opposed to reloading only on .go file change",
},
cli.StringFlag{
Name: "buildArgs",
Usage: "Additional go build arguments",
},
cli.StringFlag{
Name: "logPrefix",
Usage: "Log prefix",
Value: "goreload",
},
}
app.Commands = []cli.Command{
{
Name: "run",
ShortName: "r",
Usage: "Run the goreload",
Action: mainAction,
},
}
app.Run(os.Args)
}
func mainAction(c *cli.Context) {
all := c.GlobalBool("all")
logPrefix := c.GlobalString("logPrefix")
logger.SetPrefix(fmt.Sprintf("[%s] ", logPrefix))
wd, err := os.Getwd()
if err != nil {
logger.Fatal(err)
}
buildArgs, err := shellwords.Parse(c.GlobalString("buildArgs"))
if err != nil {
logger.Fatal(err)
}
buildPath := c.GlobalString("build")
if buildPath == "" {
buildPath = c.GlobalString("path")
}
builder := internal.NewBuilder(buildPath, c.GlobalString("bin"), wd, buildArgs)
runner := internal.NewRunner(filepath.Join(wd, builder.Binary()), c.Args()...)
runner.SetWriter(os.Stdout)
shutdown(runner)
// build right now
build(builder, runner, logger)
// scan for changes
scanChanges(c.GlobalString("path"), c.GlobalStringSlice("excludeDir"), all, func() {
runner.Kill()
build(builder, runner, logger)
})
}
func build(builder internal.Builder, runner internal.Runner, logger *log.Logger) {
logger.Println("Building...")
err := builder.Build()
if err != nil {
logger.Printf("%sBuild failed%s\n", colorRed, colorReset)
fmt.Println(builder.Errors())
} else {
logger.Printf("%sBuild finished%s\n", colorGreen, colorReset)
runner.Run()
}
time.Sleep(100 * time.Millisecond)
}
func scanChanges(watchPath string, excludeDirs []string, allFiles bool, cb func()) {
for {
filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if path == ".git" && info.IsDir() {
return filepath.SkipDir
}
for _, x := range excludeDirs {
if x == path {
return filepath.SkipDir
}
}
// ignore hidden files
if filepath.Base(path)[0] == '.' {
return nil
}
if (allFiles || filepath.Ext(path) == ".go") && info.ModTime().After(startTime) {
cb()
startTime = time.Now()
return errors.New("done")
}
return nil
})
time.Sleep(500 * time.Millisecond)
}
}
func shutdown(runner internal.Runner) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
log.Println("Got signal: ", s)
err := runner.Kill()
if err != nil {
log.Print("Error killing: ", err)
}
os.Exit(1)
}()
}