mkdoc.zsh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env zsh
  2. [ "${ZSH_VERSION:-}" = "" ] && echo >&2 "Only works with zsh" && exit 1
  3. setopt err_exit no_unset pipefail extended_glob
  4. # Simple script to update the godoc comments on all watchers. Probably took me
  5. # more time to write this than doing it manually, but ah well 🙃
  6. watcher=$(<<EOF
  7. // Watcher watches a set of paths, delivering events on a channel.
  8. //
  9. // A watcher should not be copied (e.g. pass it by pointer, rather than by
  10. // value).
  11. //
  12. // # Linux notes
  13. //
  14. // When a file is removed a Remove event won't be emitted until all file
  15. // descriptors are closed, and deletes will always emit a Chmod. For example:
  16. //
  17. // fp := os.Open("file")
  18. // os.Remove("file") // Triggers Chmod
  19. // fp.Close() // Triggers Remove
  20. //
  21. // This is the event that inotify sends, so not much can be changed about this.
  22. //
  23. // The fs.inotify.max_user_watches sysctl variable specifies the upper limit
  24. // for the number of watches per user, and fs.inotify.max_user_instances
  25. // specifies the maximum number of inotify instances per user. Every Watcher you
  26. // create is an "instance", and every path you add is a "watch".
  27. //
  28. // These are also exposed in /proc as /proc/sys/fs/inotify/max_user_watches and
  29. // /proc/sys/fs/inotify/max_user_instances
  30. //
  31. // To increase them you can use sysctl or write the value to the /proc file:
  32. //
  33. // # Default values on Linux 5.18
  34. // sysctl fs.inotify.max_user_watches=124983
  35. // sysctl fs.inotify.max_user_instances=128
  36. //
  37. // To make the changes persist on reboot edit /etc/sysctl.conf or
  38. // /usr/lib/sysctl.d/50-default.conf (details differ per Linux distro; check
  39. // your distro's documentation):
  40. //
  41. // fs.inotify.max_user_watches=124983
  42. // fs.inotify.max_user_instances=128
  43. //
  44. // Reaching the limit will result in a "no space left on device" or "too many open
  45. // files" error.
  46. //
  47. // # kqueue notes (macOS, BSD)
  48. //
  49. // kqueue requires opening a file descriptor for every file that's being watched;
  50. // so if you're watching a directory with five files then that's six file
  51. // descriptors. You will run in to your system's "max open files" limit faster on
  52. // these platforms.
  53. //
  54. // The sysctl variables kern.maxfiles and kern.maxfilesperproc can be used to
  55. // control the maximum number of open files, as well as /etc/login.conf on BSD
  56. // systems.
  57. //
  58. // # macOS notes
  59. //
  60. // Spotlight indexing on macOS can result in multiple events (see [#15]). A
  61. // temporary workaround is to add your folder(s) to the "Spotlight Privacy
  62. // Settings" until we have a native FSEvents implementation (see [#11]).
  63. //
  64. // [#11]: https://github.com/fsnotify/fsnotify/issues/11
  65. // [#15]: https://github.com/fsnotify/fsnotify/issues/15
  66. EOF
  67. )
  68. new=$(<<EOF
  69. // NewWatcher creates a new Watcher.
  70. EOF
  71. )
  72. add=$(<<EOF
  73. // Add starts monitoring the path for changes.
  74. //
  75. // A path can only be watched once; attempting to watch it more than once will
  76. // return an error. Paths that do not yet exist on the filesystem cannot be
  77. // added. A watch will be automatically removed if the path is deleted.
  78. //
  79. // A path will remain watched if it gets renamed to somewhere else on the same
  80. // filesystem, but the monitor will get removed if the path gets deleted and
  81. // re-created, or if it's moved to a different filesystem.
  82. //
  83. // Notifications on network filesystems (NFS, SMB, FUSE, etc.) or special
  84. // filesystems (/proc, /sys, etc.) generally don't work.
  85. //
  86. // # Watching directories
  87. //
  88. // All files in a directory are monitored, including new files that are created
  89. // after the watcher is started. Subdirectories are not watched (i.e. it's
  90. // non-recursive).
  91. //
  92. // # Watching files
  93. //
  94. // Watching individual files (rather than directories) is generally not
  95. // recommended as many tools update files atomically. Instead of "just" writing
  96. // to the file a temporary file will be written to first, and if successful the
  97. // temporary file is moved to to destination removing the original, or some
  98. // variant thereof. The watcher on the original file is now lost, as it no
  99. // longer exists.
  100. //
  101. // Instead, watch the parent directory and use Event.Name to filter out files
  102. // you're not interested in. There is an example of this in [cmd/fsnotify/file.go].
  103. EOF
  104. )
  105. remove=$(<<EOF
  106. // Remove stops monitoring the path for changes.
  107. //
  108. // Directories are always removed non-recursively. For example, if you added
  109. // /tmp/dir and /tmp/dir/subdir then you will need to remove both.
  110. //
  111. // Removing a path that has not yet been added returns [ErrNonExistentWatch].
  112. EOF
  113. )
  114. close=$(<<EOF
  115. // Close removes all watches and closes the events channel.
  116. EOF
  117. )
  118. watchlist=$(<<EOF
  119. // WatchList returns all paths added with [Add] (and are not yet removed).
  120. EOF
  121. )
  122. events=$(<<EOF
  123. // Events sends the filesystem change events.
  124. //
  125. // fsnotify can send the following events; a "path" here can refer to a
  126. // file, directory, symbolic link, or special file like a FIFO.
  127. //
  128. // fsnotify.Create A new path was created; this may be followed by one
  129. // or more Write events if data also gets written to a
  130. // file.
  131. //
  132. // fsnotify.Remove A path was removed.
  133. //
  134. // fsnotify.Rename A path was renamed. A rename is always sent with the
  135. // old path as Event.Name, and a Create event will be
  136. // sent with the new name. Renames are only sent for
  137. // paths that are currently watched; e.g. moving an
  138. // unmonitored file into a monitored directory will
  139. // show up as just a Create. Similarly, renaming a file
  140. // to outside a monitored directory will show up as
  141. // only a Rename.
  142. //
  143. // fsnotify.Write A file or named pipe was written to. A Truncate will
  144. // also trigger a Write. A single "write action"
  145. // initiated by the user may show up as one or multiple
  146. // writes, depending on when the system syncs things to
  147. // disk. For example when compiling a large Go program
  148. // you may get hundreds of Write events, so you
  149. // probably want to wait until you've stopped receiving
  150. // them (see the dedup example in cmd/fsnotify).
  151. //
  152. // fsnotify.Chmod Attributes were changed. On Linux this is also sent
  153. // when a file is removed (or more accurately, when a
  154. // link to an inode is removed). On kqueue it's sent
  155. // and on kqueue when a file is truncated. On Windows
  156. // it's never sent.
  157. EOF
  158. )
  159. errors=$(<<EOF
  160. // Errors sends any errors.
  161. EOF
  162. )
  163. set-cmt() {
  164. local pat=$1
  165. local cmt=$2
  166. IFS=$'\n' local files=($(grep -n $pat backend_*~*_test.go))
  167. for f in $files; do
  168. IFS=':' local fields=($=f)
  169. local file=$fields[1]
  170. local end=$(( $fields[2] - 1 ))
  171. # Find start of comment.
  172. local start=0
  173. IFS=$'\n' local lines=($(head -n$end $file))
  174. for (( i = 1; i <= $#lines; i++ )); do
  175. local line=$lines[-$i]
  176. if ! grep -q '^[[:space:]]*//' <<<$line; then
  177. start=$(( end - (i - 2) ))
  178. break
  179. fi
  180. done
  181. head -n $(( start - 1 )) $file >/tmp/x
  182. print -r -- $cmt >>/tmp/x
  183. tail -n+$(( end + 1 )) $file >>/tmp/x
  184. mv /tmp/x $file
  185. done
  186. }
  187. set-cmt '^type Watcher struct ' $watcher
  188. set-cmt '^func NewWatcher(' $new
  189. set-cmt '^func (w \*Watcher) Add(' $add
  190. set-cmt '^func (w \*Watcher) Remove(' $remove
  191. set-cmt '^func (w \*Watcher) Close(' $close
  192. set-cmt '^func (w \*Watcher) WatchList(' $watchlist
  193. set-cmt '^[[:space:]]*Events *chan Event$' $events
  194. set-cmt '^[[:space:]]*Errors *chan error$' $errors