notify.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package watch
  14. import (
  15. "expvar"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strconv"
  20. )
  21. var numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
  22. type FileEvent string
  23. func NewFileEvent(p string) FileEvent {
  24. if !filepath.IsAbs(p) {
  25. panic(fmt.Sprintf("NewFileEvent only accepts absolute paths. Actual: %s", p))
  26. }
  27. return FileEvent(p)
  28. }
  29. type Notify interface {
  30. // Start watching the paths set at init time
  31. Start() error
  32. // Stop watching and close all channels
  33. Close() error
  34. // A channel to read off incoming file changes
  35. Events() chan FileEvent
  36. // A channel to read off show-stopping errors
  37. Errors() chan error
  38. }
  39. // When we specify directories to watch, we often want to
  40. // ignore some subset of the files under those directories.
  41. //
  42. // For example:
  43. // - Watch /src/repo, but ignore /src/repo/.git
  44. // - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary
  45. //
  46. // The PathMatcher interface helps us manage these ignores.
  47. type PathMatcher interface {
  48. Matches(file string) (bool, error)
  49. // If this matches the entire dir, we can often optimize filetree walks a bit.
  50. MatchesEntireDir(file string) (bool, error)
  51. }
  52. // AnyMatcher is a PathMatcher to match any path
  53. type AnyMatcher struct{}
  54. func (AnyMatcher) Matches(f string) (bool, error) { return true, nil }
  55. func (AnyMatcher) MatchesEntireDir(f string) (bool, error) { return true, nil }
  56. var _ PathMatcher = AnyMatcher{}
  57. // EmptyMatcher is a PathMatcher to match no path
  58. type EmptyMatcher struct{}
  59. func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
  60. func (EmptyMatcher) MatchesEntireDir(f string) (bool, error) { return false, nil }
  61. var _ PathMatcher = EmptyMatcher{}
  62. func NewWatcher(paths []string) (Notify, error) {
  63. return newWatcher(paths)
  64. }
  65. const WindowsBufferSizeEnvVar = "COMPOSE_WATCH_WINDOWS_BUFFER_SIZE"
  66. const defaultBufferSize int = 65536
  67. func DesiredWindowsBufferSize() int {
  68. envVar := os.Getenv(WindowsBufferSizeEnvVar)
  69. if envVar != "" {
  70. size, err := strconv.Atoi(envVar)
  71. if err == nil {
  72. return size
  73. }
  74. }
  75. return defaultBufferSize
  76. }
  77. type CompositePathMatcher struct {
  78. Matchers []PathMatcher
  79. }
  80. func NewCompositeMatcher(matchers ...PathMatcher) PathMatcher {
  81. if len(matchers) == 0 {
  82. return EmptyMatcher{}
  83. }
  84. return CompositePathMatcher{Matchers: matchers}
  85. }
  86. func (c CompositePathMatcher) Matches(f string) (bool, error) {
  87. for _, t := range c.Matchers {
  88. ret, err := t.Matches(f)
  89. if err != nil {
  90. return false, err
  91. }
  92. if ret {
  93. return true, nil
  94. }
  95. }
  96. return false, nil
  97. }
  98. func (c CompositePathMatcher) MatchesEntireDir(f string) (bool, error) {
  99. for _, t := range c.Matchers {
  100. matches, err := t.MatchesEntireDir(f)
  101. if matches || err != nil {
  102. return matches, err
  103. }
  104. }
  105. return false, nil
  106. }
  107. var _ PathMatcher = CompositePathMatcher{}