notify.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package watch
  2. import (
  3. "expvar"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/windmilleng/tilt/internal/logger"
  7. )
  8. var (
  9. numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
  10. )
  11. type FileEvent struct {
  12. path string
  13. }
  14. func NewFileEvent(p string) FileEvent {
  15. if !filepath.IsAbs(p) {
  16. panic(fmt.Sprintf("NewFileEvent only accepts absolute paths. Actual: %s", p))
  17. }
  18. return FileEvent{path: p}
  19. }
  20. func (e FileEvent) Path() string {
  21. return e.path
  22. }
  23. type Notify interface {
  24. // Start watching the paths set at init time
  25. Start() error
  26. // Stop watching and close all channels
  27. Close() error
  28. // A channel to read off incoming file changes
  29. Events() chan FileEvent
  30. // A channel to read off show-stopping errors
  31. Errors() chan error
  32. }
  33. // When we specify directories to watch, we often want to
  34. // ignore some subset of the files under those directories.
  35. //
  36. // For example:
  37. // - Watch /src/repo, but ignore /src/repo/.git
  38. // - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary
  39. //
  40. // The PathMatcher inteface helps us manage these ignores.
  41. // By design, fileutils.PatternMatcher (the interface that implements dockerignore)
  42. // satisfies this interface
  43. // https://godoc.org/github.com/docker/docker/pkg/fileutils#PatternMatcher
  44. type PathMatcher interface {
  45. Matches(file string) (bool, error)
  46. Exclusions() bool
  47. }
  48. type EmptyMatcher struct {
  49. }
  50. func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
  51. func (EmptyMatcher) Exclusions() bool { return false }
  52. var _ PathMatcher = EmptyMatcher{}
  53. func NewWatcher(paths []string, ignore PathMatcher, l logger.Logger) (Notify, error) {
  54. return newWatcher(paths, ignore, l)
  55. }