-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
71 lines (66 loc) · 1.28 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
package main
import (
"context"
"os"
"os/exec"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func main() {
l := logrus.New()
if os.Getenv("DEBUG") == "true" {
l.Level = logrus.DebugLevel
}
if err := run(l); err != nil {
l.Fatalf("%+v", err)
}
}
func run(l logrus.FieldLogger) error {
if len(os.Args) < 2 {
return errors.New("at least 2 parameters required")
}
path, err := exec.LookPath(os.Args[1])
if err != nil {
return errors.WithStack(err)
}
options := []string{}
if len(os.Args) > 2 {
options = os.Args[2:]
}
for {
err := func() error {
ctx, cf := context.WithCancel(context.Background())
defer cf()
w, err := fsnotify.NewWatcher()
if err != nil {
return errors.WithStack(err)
}
defer w.Close()
err = w.Add(path)
if err != nil {
return errors.WithStack(err)
}
l.Printf("starting up")
go func() {
event := <-w.Events
l.Debug(event)
cf()
}()
c := exec.CommandContext(ctx, path, options...)
c.Stdout = os.Stdout
c.Stdin = os.Stdin
c.Stderr = os.Stderr
return c.Run()
}()
if err != nil {
if !strings.Contains(err.Error(), "signal: killed") {
l.Warnf("%T %v", err, err)
}
time.Sleep(100 * time.Millisecond)
}
}
return nil
}