示例#1
0
文件: watch.go 项目: ceram1/gonotify
func waitForWatcher(ctx context.Context, watcher *inotify.Watcher, match matcherFunc, tpl *template.Template, monitor bool) {
	defer watcher.Close()

	for {
		select {
		case <-ctx.Done(): // Timeout
			os.Exit(2)
			return

		case ev := <-watcher.Event:
			if !match(ev) {
				break
			}
			if tpl != nil {
				tpl.Execute(os.Stdout, ev)
				log.Infoln()
			} else {
				log.Infoln(ev)
			}
			// Finish if not monitoring mode.
			if !monitor {
				return
			}

		case err := <-watcher.Error:
			log.Fatal(err)
		}
	}
}