Advertisement
kosx

Google PGK all.bash

Dec 30th, 2020
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.47 KB | None | 0 0
  1.  
  2. #!/usr/bin/env bash
  3. # Copyright 2019 The Go Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file.
  6. source devtools/lib.sh || { echo "Are you at repo root?"; exit 1; }
  7. # Support ** in globs, for check_script_hashes.
  8. shopt -s globstar
  9. warnout() {
  10.   while read line; do
  11.     warn "$line"
  12.   done
  13. }
  14. # codedirs lists directories that contain discovery code. If they include
  15. # directories containing external code, those directories must be excluded in
  16. # findcode below.
  17. codedirs=(
  18.   "cmd"
  19.   "content"
  20.   "internal"
  21.   "migrations"
  22. )
  23. # verify_header checks that all given files contain the standard header for Go
  24. # projects.
  25. verify_header() {
  26.   if [[ "$@" != "" ]]; then
  27.     for FILE in $@
  28.     do
  29.         # Allow for the copyright header to start on either of the first two
  30.         # lines, to accommodate conventions for CSS and HTML.
  31.         line="$(head -3 $FILE)"
  32.         if [[ ! $line == *"The Go Authors. All rights reserved."* ]] &&
  33.          [[ ! $line == "// DO NOT EDIT. This file was copied from" ]]; then
  34.               err "missing license header: $FILE"
  35.         fi
  36.     done
  37.   fi
  38. }
  39. # findcode finds source files in the repo, skipping third-party source.
  40. findcode() {
  41.   find ${codedirs[@]} \
  42.     -not -path '*/third_party/*' \
  43.     \( -name *.go -o -name *.sql -o -name *.tmpl -o -name *.css -o -name *.js \)
  44. }
  45. # ensure_go_binary verifies that a binary exists in $PATH corresponding to the
  46. # given go-gettable URI. If no such binary exists, it is fetched via `go get`.
  47. ensure_go_binary() {
  48.   local binary=$(basename $1)
  49.   if ! [ -x "$(command -v $binary)" ]; then
  50.     info "Installing: $1"
  51.     # Run in a subshell for convenience, so that we don't have to worry about
  52.     # our PWD.
  53.     (set -x; cd && env GO111MODULE=on go get -u $1)
  54.   fi
  55. }
  56. # check_headers checks that all source files that have been staged in this
  57. # commit, and all other non-third-party files in the repo, have a license
  58. # header.
  59. check_headers() {
  60.   info "Checking staged files for license header"
  61.   # Check code files that have been modified or added.
  62.   verify_header $(git diff --cached --name-status | grep -vE "^D" | cut -f 2- | grep -E ".go$|.sql$|.sh$")
  63.   info "Checking internal files for license header"
  64.   verify_header $(findcode)
  65. }
  66. # bad_migrations outputs migrations with bad sequence numbers.
  67. bad_migrations() {
  68.   ls migrations | cut -d _ -f 1 | sort | uniq -c | grep -vE '^\s+2 '
  69. }
  70. # check_bad_migrations looks for sql migration files with bad sequence numbers,
  71. # possibly resulting from a bad merge.
  72. check_bad_migrations() {
  73.   info "Checking for bad migrations"
  74.   bad_migrations | while read line
  75.   do
  76.     err "unexpected number of migrations: $line"
  77.   done
  78. }
  79. # check_unparam runs unparam on source files.
  80. check_unparam() {
  81.   ensure_go_binary mvdan.cc/unparam
  82.   runcmd unparam ./...
  83. }
  84. # check_vet runs go vet on source files.
  85. check_vet() {
  86.   runcmd go vet -all ./...
  87. }
  88. # check_staticcheck runs staticcheck on source files.
  89. check_staticcheck() {
  90.   ensure_go_binary honnef.co/go/tools/cmd/staticcheck
  91.   runcmd staticcheck $(go list ./... | grep -v third_party | grep -v internal/doc | grep -v internal/render) | warnout
  92. }
  93. # check_misspell runs misspell on source files.
  94. check_misspell() {
  95.   ensure_go_binary github.com/client9/misspell/cmd/misspell
  96.   runcmd misspell cmd/**/*.{go,sh} internal/**/* README.md | warnout
  97. }
  98. # check_templates runs go-template-lint on template files. Unfortunately it
  99. # doesn't handler the /helpers/ fileglob correctly, so it is too noisy to be
  100. # included in standard checks.
  101. check_templates() {
  102.   ensure_go_binary sourcegraph.com/sourcegraph/go-template-lint
  103.   runcmd go-template-lint \
  104.     -f=internal/frontend/server.go \
  105.     -t=internal/frontend/server.go \
  106.     -td=content/static/html/pages | warnout
  107. }
  108. # check_script_hashes checks that our CSP hashes match the ones
  109. # for our HTML scripts.
  110. check_script_hashes() {
  111.   runcmd go run ./devtools/cmd/csphash content/static/html/**/*.tmpl
  112. }
  113. # run_prettier runs prettier on CSS, JS, and MD files. Uses globally
  114. # installed prettier if available or a dockerized installation as a
  115. # fallback.
  116. run_prettier() {
  117.   FILES='content/static/**/*.{js,css} **/*.md'
  118.   if [[ -x "$(command -v prettier)" ]]; then
  119.     runcmd prettier --write $FILES
  120.   elif [[ -x "$(command -v docker-compose)" && "$(docker images -q pkgsite_npm)" ]]; then
  121.     runcmd docker-compose -f devtools/config/docker-compose.yaml run --entrypoint=npx \
  122.     npm prettier --write $FILES
  123.   else
  124.     err "prettier must be installed: see https://prettier.io/docs/en/install.html"
  125.   fi
  126. }
  127. standard_linters() {
  128.   check_headers
  129.   check_bad_migrations
  130.   check_vet
  131.   check_staticcheck
  132.   check_misspell
  133.   check_unparam
  134.   check_script_hashes
  135. }
  136. usage() {
  137.   cat <<EOUSAGE
  138. Usage: $0 [subcommand]
  139. Available subcommands:
  140.   help        - display this help message
  141.   (empty)     - run all standard checks and tests
  142.   ci          - run checks and tests suitable for continuous integration
  143.   lint        - run all standard linters below:
  144.   headers     - (lint) check source files for the license disclaimer
  145.   migrations  - (lint) check migration sequence numbers
  146.   misspell    - (lint) run misspell on source files
  147.   staticcheck - (lint) run staticcheck on source files
  148.   unparam     - (lint) run unparam on source files
  149.   prettier    - (lint, nonstandard) run prettier on .js and .css files.
  150.   templates   - (lint, nonstandard) run go-template-lint on templates
  151.   script_hashses - (lint) check script hashes
  152. EOUSAGE
  153. }
  154. main() {
  155.   case "$1" in
  156.     "-h" | "--help" | "help")
  157.       usage
  158.       exit 0
  159.       ;;
  160.     "")
  161.       standard_linters
  162.       run_prettier
  163.       runcmd go mod tidy
  164.       runcmd env GO_DISCOVERY_TESTDB=true go test ./...
  165.       runcmd go test ./internal/secrets
  166.       ;;
  167.     ci)
  168.       # Similar to the no-arg mode, but omit actions that require GCP
  169.       # permissions or that don't test the code.
  170.       standard_linters
  171.       runcmd env GO_DISCOVERY_TESTDB=true go test -race -count=1 ./...
  172.       ;;
  173.     lint) standard_linters ;;
  174.     headers) check_headers ;;
  175.     migrations) check_migrations ;;
  176.     misspell) check_misspell ;;
  177.     staticcheck) check_staticcheck ;;
  178.     prettier) run_prettier ;;
  179.     templates) check_templates ;;
  180.     unparam) check_unparam ;;
  181.     script_hashes) check_script_hashes ;;
  182.     *)
  183.       usage
  184.       exit 1
  185.   esac
  186.   if [[ $EXIT_CODE != 0 ]]; then
  187.     err "FAILED; see errors above"
  188.   fi
  189.   exit $EXIT_CODE
  190. }
  191. main $@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement