cache_go_dirs.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script finds, caches, and prints a list of all directories that hold
  16. # *.go files. If any directory is newer than the cache, re-find everything and
  17. # update the cache. Otherwise use the cached file.
  18. set -o errexit
  19. set -o nounset
  20. set -o pipefail
  21. if [[ -z "${1:-}" ]]; then
  22. echo "usage: $0 <cache-file>"
  23. exit 1
  24. fi
  25. CACHE="$1"; shift
  26. trap "rm -f '${CACHE}'" HUP INT TERM ERR
  27. # This is a partial 'find' command. The caller is expected to pass the
  28. # remaining arguments.
  29. #
  30. # Example:
  31. # kfind -type f -name foobar.go
  32. function kfind() {
  33. find . \
  34. -not \( \
  35. \( \
  36. -path ./vendor -o \
  37. -path ./staging -o \
  38. -path ./_\* -o \
  39. -path ./.\* -o \
  40. -path ./docs -o \
  41. -path ./examples \
  42. \) -prune \
  43. \) \
  44. "$@"
  45. }
  46. NEED_FIND=true
  47. # It's *significantly* faster to check whether any directories are newer than
  48. # the cache than to blindly rebuild it.
  49. if [[ -f "${CACHE}" ]]; then
  50. N=$(kfind -type d -newer "${CACHE}" -print -quit | wc -l)
  51. [[ "${N}" == 0 ]] && NEED_FIND=false
  52. fi
  53. mkdir -p $(dirname "${CACHE}")
  54. if $("${NEED_FIND}"); then
  55. kfind -type f -name \*.go \
  56. | xargs -n1 dirname \
  57. | sort -u \
  58. | sed 's|^./||' \
  59. > "${CACHE}"
  60. fi
  61. cat "${CACHE}"