This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
144 lines (129 loc) · 3.5 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
package main // import "go.sbr.pm/ram"
import (
"fmt"
"os"
"strings"
"time"
"github.com/dnephin/filewatcher/files"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"go.sbr.pm/ram/runner"
)
var (
defaultExcludes = []string{
"**/*.swp",
"**/*.swx",
"vendor",
"vendor/**",
".git",
".ignore",
".gitignore",
"**/*file",
"*.lock",
}
defaultIdleTimeout = 90 * time.Minute
)
type options struct {
verbose bool
quiet bool
exclude []string
dirs []string
depth int
command []string
idleTimeout time.Duration
events eventOpt
}
func setupFlags(args []string) *options {
flags := pflag.NewFlagSet(args[0], pflag.ContinueOnError)
flags.ParseErrorsWhitelist = pflag.ParseErrorsWhitelist{UnknownFlags: true}
opts := options{}
flags.BoolVarP(&opts.verbose, "verbose", "v", false, "Verbose")
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Quiet")
flags.StringSliceVarP(&opts.exclude, "exclude", "x", nil, "Exclude file patterns")
flags.StringSliceVarP(&opts.dirs, "directory", "d", []string{"."}, "Directories to watch")
flags.IntVarP(&opts.depth, "depth", "L", 8, "Descend only level directories deep")
flags.DurationVar(&opts.idleTimeout, "idle-timeout", defaultIdleTimeout,
"Exit after idle timeout")
flags.VarP(&opts.events, "event", "e",
"events to watch (create,write,remove,rename,chmod)")
flags.SetInterspersed(false)
flags.Usage = func() {
out := os.Stderr
fmt.Fprintf(out, "Usage:\n %s [OPTIONS] COMMAND ARGS... \n\n", os.Args[0])
fmt.Fprint(out, "Options:\n")
flags.PrintDefaults()
}
if err := flags.Parse(args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(2)
}
opts.command = flags.Args()
return &opts
}
func main() {
opts := setupFlags(os.Args)
setupLogging(opts)
run(opts)
}
func run(opts *options) {
excludes := append(defaultExcludes, opts.exclude...)
excludeList, err := files.NewExcludeList(excludes)
if err != nil {
log.Fatalf("Error creating exclude list: %s", err)
}
dirs := files.WalkDirectories(opts.dirs, opts.depth, excludeList)
watcher, err := buildWatcher(dirs)
if err != nil {
log.Fatalf("Error setting up watcher: %s", err)
}
defer watcher.Close()
log.Debugf("Handling events: %s", opts.events.Value())
command := append([]string{"go", "test"}, cleanCommand(opts.command)...)
log.Infof("Run %s", strings.Join(command, " "))
handler, cleanup := runner.NewRunner(excludeList, opts.events.Value(), command)
defer cleanup()
watchOpts := runner.WatchOptions{
IdleTimeout: opts.idleTimeout,
Runner: handler,
}
if err = runner.Watch(watcher, watchOpts); err != nil {
log.Fatalf("Error during watch: %s", err)
}
}
func cleanCommand(command []string) []string {
c := []string{}
for _, v := range command {
if v == "--" {
continue
}
c = append(c, v)
}
return append(c, "./${dir}")
}
func setupLogging(opts *options) {
formatter := new(prefixed.TextFormatter)
formatter.DisableTimestamp = true
log.SetFormatter(formatter)
if opts.verbose {
log.SetLevel(log.DebugLevel)
}
if opts.quiet {
log.SetLevel(log.WarnLevel)
}
}
func buildWatcher(dirs []string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
log.Infof("Watching directories: %s", strings.Join(dirs, ", "))
for _, dir := range dirs {
log.Debugf("Adding new watch: %s", dir)
if err = watcher.Add(dir); err != nil {
return nil, err
}
}
return watcher, nil
}