Skip to content

Commit 03cdcd8

Browse files
committed
Add --print-changes support
1 parent 1d5c2ef commit 03cdcd8

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: godemon test
2+
3+
godemon:
4+
go build -o godemon ./cmd/godemon
5+
6+
test:
7+
go test ./...

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ type Config struct {
5353
// godemon [ flags ... ] sh -c 'clear && exec {command}'
5454
Clear bool `json:"clear,omitempty"`
5555

56+
// PrintChanges specifies whether to print change events to stdout without
57+
// running any command. When enabled, no command should be specified.
58+
PrintChanges bool `json:"printChanges,omitempty"`
59+
5660
// TODO: FollowSymlinks
5761

5862
// Private fields below

godemon.go

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Advanced options:
3535
--no-gitignore disable adding patterns from .gitignore to ignore list
3636
-s, --signal restart signal name or number (default SIGINT)
3737
-v, --verbose log more info; set twice to log lower level debug info
38+
--print-changes print change events to stdout without running a command
3839
`)
3940
}
4041

@@ -97,6 +98,10 @@ func parseConfig(args []string) (*Config, error) {
9798
clearTerminal = true
9899
continue
99100
}
101+
if arg == "--print-changes" {
102+
cfg.PrintChanges = true
103+
continue
104+
}
100105

101106
// Parse string flags
102107
for _, spec := range []struct {
@@ -230,8 +235,14 @@ func parseConfig(args []string) (*Config, error) {
230235
}
231236
cfg.notifySignal = s
232237
}
233-
if len(cfg.Command) == 0 {
234-
return nil, fmt.Errorf("must specify a command to run")
238+
if cfg.PrintChanges {
239+
if len(cfg.Command) > 0 {
240+
return nil, fmt.Errorf("--print-changes cannot be used with a command")
241+
}
242+
} else {
243+
if len(cfg.Command) == 0 {
244+
return nil, fmt.Errorf("must specify a command to run")
245+
}
235246
}
236247
cfg.dryRun = dryRun
237248

@@ -619,20 +630,23 @@ func (g *godemon) Start() error {
619630

620631
shutdownCh := make(chan struct{}, 1)
621632

622-
// Command worker
623-
restart := throttleRestarts(events)
624-
625633
if g.cfg.dryRun {
626634
// Allow 1s for file watchers to be set up.
627635
// TODO: Wait till addCh settles instead.
628636
time.Sleep(1 * time.Second)
629637
notifyf("Exiting due to --dry-run flag.")
630638
}
631639

632-
go g.loopCommand(restart, shutdownCh)
633-
634-
// Wait for events and dispatch to the appropriate handler.
635-
g.handleEvents(addCh, events, shutdownCh)
640+
if g.cfg.PrintChanges {
641+
// In print-changes mode, just print events to stdout without running a command.
642+
g.handlePrintChanges(addCh, shutdownCh)
643+
} else {
644+
// Command worker
645+
restart := throttleRestarts(events)
646+
go g.loopCommand(restart, shutdownCh)
647+
// Wait for events and dispatch to the appropriate handler.
648+
g.handleEvents(addCh, events, shutdownCh)
649+
}
636650

637651
return nil
638652
}
@@ -802,6 +816,37 @@ func (g *godemon) handleEvents(addCh chan<- string, restartCh chan<- struct{}, s
802816
}
803817
}
804818

819+
func (g *godemon) handlePrintChanges(addCh chan<- string, shutdownCh <-chan struct{}) {
820+
sig := make(chan os.Signal, 4)
821+
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
822+
823+
go func() {
824+
<-sig
825+
os.Exit(0)
826+
}()
827+
828+
for {
829+
select {
830+
case err := <-g.w.Errors:
831+
warnf("%s", err)
832+
case event := <-g.w.Events:
833+
if g.shouldIgnore(event.Name) {
834+
infof("Ignoring event: %s %q", event.Op, event.Name)
835+
continue
836+
}
837+
// Print the change event to stdout
838+
fmt.Printf("%s %s\n", event.Op, event.Name)
839+
// When creating new dirs, add them to the watch list.
840+
if event.Op == fsnotify.Create && isDir(event.Name) {
841+
addCh <- event.Name
842+
}
843+
case <-shutdownCh:
844+
debugf("handlePrintChanges: got shutdown signal")
845+
return
846+
}
847+
}
848+
}
849+
805850
func waitForLockfileRemoval(path string) error {
806851
debugf("Waiting for removal of lockfile %s", path)
807852

godemon_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12-
"runtime"
1312
"strconv"
1413
"strings"
1514
"syscall"

0 commit comments

Comments
 (0)