NewDirectoryWatcher returns a DirWatcher instance. The DirWatcher watches the given directory for file system events. Currently, only events on files directly in the given directory are watched, not recursively. Note that the returned DirWatcher is not ready to use until the goroutine started by N
(parent task.Parent, dirPath string)
| 37 | // Note that the returned DirWatcher is not ready to use until the goroutine |
| 38 | // started by NewDirectoryWatcher has finished. |
| 39 | func NewDirectoryWatcher(parent task.Parent, dirPath string) *DirWatcher { |
| 40 | //! subdirectories are not watched |
| 41 | w, err := fsnotify.NewWatcher() |
| 42 | if err != nil { |
| 43 | log.Panic().Err(err).Msg("unable to create fs watcher") |
| 44 | } |
| 45 | if err = w.Add(dirPath); err != nil { |
| 46 | log.Panic().Err(err).Msg("unable to create fs watcher") |
| 47 | } |
| 48 | helper := &DirWatcher{ |
| 49 | Logger: log.With(). |
| 50 | Str("type", "dir"). |
| 51 | Str("path", dirPath). |
| 52 | Logger(), |
| 53 | dir: dirPath, |
| 54 | w: w, |
| 55 | fwMap: make(map[string]*fileWatcher), |
| 56 | eventCh: make(chan Event), |
| 57 | errCh: make(chan error), |
| 58 | task: parent.Subtask("dir_watcher("+dirPath+")", true), |
| 59 | } |
| 60 | go helper.start() |
| 61 | return helper |
| 62 | } |
| 63 | |
| 64 | var _ Watcher = (*DirWatcher)(nil) |
| 65 |
no test coverage detected