-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (94 loc) · 2.22 KB
/
Copy pathmain.go
File metadata and controls
104 lines (94 loc) · 2.22 KB
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
package main
import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
// Version gets overridden at build time using -X main.Version=$VERSION
var (
Version = "dev"
)
func init() {
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.InfoLevel)
}
func main() {
app := &cli.App{
Name: "nightwatch",
Version: Version,
Usage: "A utility for running arbitrary commands when files change",
Before: func(ctx *cli.Context) error {
if ctx.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
},
Action: func(c *cli.Context) error {
if !c.Args().Present() {
logrus.Fatal("No command specified")
}
done := make(chan os.Signal, 2)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
exitOnChange := -1
if c.IsSet("exit-on-change") {
exitOnChange = c.Int("exit-on-change")
}
filesList := []string{}
if c.IsSet("files") {
filesList = strings.Split(c.String("files"), ",")
}
nightWatch := &NightWatch{
cmdSignal: make(chan *processSignal, 1),
args: c.Args(),
exitOnChange: exitOnChange,
exitOnError: c.Bool("exit-on-error"),
exitOnSuccess: c.Bool("exit-on-success"),
watchCmd: c.String("find-cmd"),
filesList: filesList,
}
go nightWatch.Run()
exitSignal := <-done
exitCode := nightWatch.Stop(exitSignal.(syscall.Signal))
time.Sleep(10 * time.Second)
nightWatch.Cleanup()
os.Exit(exitCode)
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Debug logging.",
},
&cli.StringFlag{
Name: "find-cmd",
Usage: "Command to list files (or dirs) to watch",
Value: "find . -type f -not -path '*/\\.git/*'",
},
&cli.StringFlag{
Name: "files",
Usage: "Files (or dirs) to watch (comma separated list)",
},
&cli.IntFlag{
Name: "exit-on-error",
Usage: "Exit if process returns an error code.",
},
&cli.IntFlag{
Name: "exit-on-success",
Usage: "Exit if process returns with code 0.",
},
&cli.IntFlag{
Name: "exit-on-change",
Usage: "Exit on file change with a given code.",
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}