forked from gookit/gcli
/
file_watcher.go
178 lines (147 loc) · 3.79 KB
/
file_watcher.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
172
173
174
175
176
177
178
package filewatcher
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/gookit/cliapp"
"github.com/gookit/color"
"os"
"path/filepath"
"strings"
)
var watcher *fsnotify.Watcher
var opts = struct {
Dir string
Ext string
Files cliapp.Strings
Config string
Exclude cliapp.Strings
handler func(event fsnotify.Event)
}{}
// FileWatcher command definition
func FileWatcher(handler func(event fsnotify.Event)) *cliapp.Command {
cmd := &cliapp.Command{
Name: "watch",
Func: watch,
UseFor: "file system change notification, by fsnotify",
Aliases: []string{"fwatch", "fswatch"},
Examples: `watch a dir:
{$fullCmd} -e .git -e .idea -d ./_examples --ext ".go|.md"
watch a file(s):
{$fullCmd} -f _examples/cliapp.go -f app.go
open debug mode:
{$binName} --verbose 4 {$cmd} -e .git -e .idea -d ./_examples --ext ".go|.md"
`,
}
cmd.StrOpt(&opts.Dir, "dir", "d", "", "the want watched directory")
cmd.StrOpt(&opts.Ext, "ext", "", ".go", "the watched file extensions, multi split by '|'")
cmd.VarOpt(&opts.Files, "files", "f", "the want watched file paths")
cmd.StrOpt(&opts.Config, "config", "c", "", "load options from a json config")
cmd.VarOpt(&opts.Exclude, "exclude", "e", "the ignored directory or files")
opts.handler = handler
return cmd
}
// test run:
// go run ./_examples/cliapp.go watch -e .git -e .idea -d ./_examples
func watch(c *cliapp.Command, _ []string) int {
color.Info.Println("Work directory: ", c.WorkDir())
if opts.Dir == "" && len(opts.Files) == 0 {
return c.Errorf("watched directory or files cannot be empty")
}
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
return c.WithError(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
c.Logf(cliapp.VerbInfo, "event: %s", event)
if event.Op&fsnotify.Write == fsnotify.Write {
c.Logf(cliapp.VerbDebug, "modified file: %s", event.Name)
}
if opts.handler != nil {
opts.handler(event)
}
case err := <-watcher.Errors:
c.Logf(cliapp.VerbError, "error: %s", err.Error())
}
}
}()
if len(opts.Files) > 0 {
if err = addWatchFiles(opts.Files); err != nil {
// <-done
return c.WithError(err)
}
}
if opts.Dir != "" {
fmt.Println("- add watch dir: ", color.FgGreen.Render(opts.Dir))
if err = addWatchDir(opts.Dir); err != nil {
return c.WithError(err)
}
}
<-done
return 0
}
func addWatchFiles(files []string) error {
for _, path := range files {
cliapp.Logf(cliapp.VerbDebug, "add watch file: %s", path)
err := watcher.Add(path)
if err != nil {
return err
}
}
return nil
}
func addWatchDir(dir string) error {
allowExt := ""
if opts.Ext != "" {
// always wrap char "|". eg ".go|.md" -> "|.go|.md|"
allowExt = "|" + opts.Ext + "|"
}
// filepath.Match()
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil { // continue
return err
}
// get base name.
// /path/dir -> dir
// /path/file.ext -> file.ext
name := filepath.Base(path)
if isExclude(name) { // skip
return nil
}
if info.IsDir() {
err = watcher.Add(path)
cliapp.Logf(cliapp.VerbDebug, "add watch dir: %s", path)
return err // continue OR err
}
// has ext limit
if allowExt != "" {
// get ext. eg ".go"
ext := filepath.Ext(path)
if strings.Contains(allowExt, "|"+ext+"|") {
// add file watch
err = watcher.Add(path)
cliapp.Logf(cliapp.VerbDebug, "add watch file: %s", path)
}
} else { // add any file
err = watcher.Add(path)
cliapp.Logf(cliapp.VerbDebug, "add watch file: %s", path)
}
return err
})
}
func isExclude(name string) bool {
if len(opts.Exclude) == 0 {
return false
}
for _, exclude := range opts.Exclude {
if exclude == name {
return true
}
}
return false
}