@@ -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+
805850func waitForLockfileRemoval (path string ) error {
806851 debugf ("Waiting for removal of lockfile %s" , path )
807852
0 commit comments