Skip to content

Commit

Permalink
filter option
Browse files Browse the repository at this point in the history
  • Loading branch information
makiuchi-d committed Oct 29, 2023
1 parent 14a63d5 commit 04cad1f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Run the COMMAND and restart when a file matches the pattern has been modified.
Options:
-d, --delay duration duration to delay the restart of the command (default 1s)
-f, --filter event filter file system event (CREATE|WRITE|REMOVE|RENAME|CHMOD)
-h, --help display this message
-i, --ignore glob ignore pathname glob pattern
-p, --pattern glob trigger pathname glob pattern (default "**")
Expand Down Expand Up @@ -92,6 +93,14 @@ This option takes precedence over the --pattern option.
This option can set multiple times.


#### -f, --filter event

Filter the filesystem event to ignore it.

The event can be `CREATE`, `WRITE`, `REMOVE`, `RENAME` or `CHMOD`.

This option can set multiple times.

#### -d, --delay duration

Delay the restart of the command from the detection of the pattern matched file modification.
Expand Down Expand Up @@ -153,3 +162,4 @@ Command to run.
- [gin](https://github.com/codegangsta/gin)
- [go-task](https://github.com/go-task/task)
- [air](https://github.com/cosmtrek/air)
- [reflex](https://github.com/cespare/reflex)
46 changes: 38 additions & 8 deletions arelo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Options:
verbose = pflag.BoolP("verbose", "v", false, "verbose output")
help = pflag.BoolP("help", "h", false, "display this message")
showver = pflag.BoolP("version", "V", false, "display version")
filters = pflag.StringArrayP("filter", "f", nil, "filter file system `event` (CREATE|WRITE|REMOVE|RENAME|CHMOD)")
)

func main() {
Expand All @@ -59,10 +60,15 @@ func main() {
*patterns = []string{"**"}
}
sig, sigstr := parseSignalOption(*sigopt)
filtOp, err := parseFileters(*filters)
if err != nil {
log.Fatalf("[ARELO] %v", err)
}
logVerbose("command: %q", cmd)
logVerbose("targets: %q", *targets)
logVerbose("patterns: %q", *patterns)
logVerbose("ignores: %q", *ignores)
logVerbose("filter: %v", filtOp)
logVerbose("delay: %v", delay)
logVerbose("signal: %s", sigstr)
logVerbose("restart: %v", *restart)
Expand All @@ -83,7 +89,7 @@ func main() {
os.Exit(1)
}

modC, errC, err := watcher(*targets, *patterns, *ignores)
modC, errC, err := watcher(*targets, *patterns, *ignores, filtOp)
if err != nil {
log.Fatalf("[ARELO] wacher error: %v", err)
}
Expand All @@ -104,7 +110,6 @@ func main() {
log.Fatalf("[ARELO] wacher closed")
return
}
logVerbose("modified: %q", name)
reload <- name
case err := <-errC:
cancel()
Expand Down Expand Up @@ -140,7 +145,28 @@ func versionstr() string {
return info.Main.Version
}

func watcher(targets, patterns, ignores []string) (<-chan string, <-chan error, error) {
func parseFileters(filters []string) (fsnotify.Op, error) {
var op fsnotify.Op
for _, f := range filters {
switch strings.ToUpper(f) {
case "CREATE":
op |= fsnotify.Create
case "WRITE":
op |= fsnotify.Write
case "REMOVE":
op |= fsnotify.Remove
case "RENAME":
op |= fsnotify.Rename
case "CHMOD":
op |= fsnotify.Chmod
default:
return 0, xerrors.Errorf("invalid filter event: %s", f)
}
}
return op, nil
}

func watcher(targets, patterns, ignores []string, filtOp fsnotify.Op) (<-chan string, <-chan error, error) {
w, err := fsnotify.NewWatcher()
if err != nil {
return nil, nil, err
Expand All @@ -152,6 +178,7 @@ func watcher(targets, patterns, ignores []string) (<-chan string, <-chan error,

modC := make(chan string)
errC := make(chan error)
watchOp := ^filtOp

go func() {
defer close(modC)
Expand All @@ -164,6 +191,7 @@ func watcher(targets, patterns, ignores []string) (<-chan string, <-chan error,
}

name := filepath.ToSlash(event.Name)
logVerbose("event: %v %q", event.Op, name)

if ignore, err := matchPatterns(name, ignores); err != nil {
errC <- xerrors.Errorf("match ignores: %w", err)
Expand All @@ -172,11 +200,13 @@ func watcher(targets, patterns, ignores []string) (<-chan string, <-chan error,
continue
}

if match, err := matchPatterns(name, patterns); err != nil {
errC <- xerrors.Errorf("match patterns: %w", err)
return
} else if match {
modC <- name
if event.Has(watchOp) {
if match, err := matchPatterns(name, patterns); err != nil {
errC <- xerrors.Errorf("match patterns: %w", err)
return
} else if match {
modC <- name
}
}

// add watcher if new directory.
Expand Down
2 changes: 1 addition & 1 deletion arelo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestWatcher(t *testing.T) {
ignores := []string{"**/ignore"}
patterns := []string{"**/file"}

modC, errC, err := watcher(targets, patterns, ignores)
modC, errC, err := watcher(targets, patterns, ignores, 0)
if err != nil {
t.Fatalf("watcher: %v", err)
}
Expand Down

0 comments on commit 04cad1f

Please sign in to comment.