-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (59 loc) · 1.51 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/thiagozs/go-reload/runner"
)
var (
dirToMonitor string
command string
excluded string
params string
)
func main() {
flag.StringVar(&dirToMonitor, "dir", ".", "Directory to monitor for changes")
flag.StringVar(&command, "cmd", "", "Command to run when a change is detected")
flag.StringVar(&excluded, "exclude", "", "Comma-separated list of directories to exclude from monitoring")
flag.StringVar(¶ms, "params", "", "Comma-separated list of parameters to pass to the command")
flag.Parse()
if command == "" {
log.Fatal("Please specify a command to run using the -cmd flag")
}
var dirExcluded []string
if strings.Contains(excluded, ",") {
norm := strings.Split(excluded, ",")
for _, v := range norm {
dirExcluded = append(dirExcluded,
fmt.Sprintf("%s/%s", dirToMonitor, v))
}
} else {
dirExcluded = append(dirExcluded,
fmt.Sprintf("%s/%s", dirToMonitor, excluded))
}
rw, err := runner.NewRealWatcher()
if err != nil {
log.Fatal(err)
}
opts := []runner.CmdOpts{
runner.DirToMonitor(dirToMonitor),
runner.Command(command),
runner.ExcludedDirs(dirExcluded),
runner.RegisterWatcher(rw),
runner.CmdParams(params),
}
r, err := runner.NewCommandRunner(opts...)
if err != nil {
log.Fatal(err)
}
go r.WatchForChanges()
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
<-done
rw.Close()
log.Println("Stopping the watcher...")
}