Debian Codegraph @

scripts/build-check-comments.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 set -eu
4
5 # Reliable cross-platform comment checker for go files.
6 # Uses docker and stable linux distro for reliable grep regex and
7 # to make sure we work on Linux and OS X and oh even Windows! :)
8 # Main issue is minor incompatibies between grep on various platforms.
9 #
10 # Linux: set USE_DOCKER=yes or USE_DOCKER=false to force run in docker
11 # All other platforms: uses docker by default. Set USE_DOCKER=no or USE_DOCKER=false to override.
12 #
13 # Checks Go code comments if they have trailing period (excludes protobuffers and vendor files).
14 # Comments with more than 3 spaces at beginning are omitted from the check, example: '// - foo'.
15 # This does not include top-level commments for funcs and types.
16 #
17 # Example:
18 # func main() {
19 # // comment without period, will trigger check
20 # //comment without leading space, will trigger check.
21 # // comment without trailing space, will trigger check.
22 # // good comment, leading space, ends with period, no trailing space.
23 # // - more than 3 leading spaces, will pass
24 # app := kingpin.New(filepath.Base(os.Args[0]), "A block storage based long-term storage for Prometheus")
25 # }
26
27
28 # Abs path to project dir and this script, should work on all OS's
29 declare ROOT_DIR="$(cd $(dirname "${BASH_SOURCE}")/.. && pwd)"
30 declare THIS_SCRIPT="$(cd $(dirname "${BASH_SOURCE}") && pwd)/$(basename "${BASH_SOURCE}")"
31
32 # Image to use if we do docker-based commands. NB: busybox is no good for this.
33 declare IMAGE="debian:9-slim"
34
35 # User can explicitly ask to run in docker
36 declare USE_DOCKER=${USE_DOCKER:=""}
37
38 # For OS X, always use Docker as we have nasty
39 # compat GNU/BSG issues with grep.
40 if test "Linux" != "$(uname || true)"
41 then
42 # Allow overriding for non-linux platforms
43 if test "no" != "${USE_DOCKER}" && test "false" != "${USE_DOCKER}"
44 then
45 USE_DOCKER="yes"
46 fi
47 fi
48
49 if test "yes" == "${USE_DOCKER}" || test "true" == "${USE_DOCKER}"
50 then
51 # Make sure we only attach TTY if we have it, CI builds won't have it.
52 declare TTY_FLAG=""
53 if [ -t 1 ]
54 then
55 TTY_FLAG="-t"
56 fi
57
58 # Annoying issue with ownership of files in mapped volumes.
59 # Need to run with same UID and GID in container as we do
60 # on the machine, otherwise all output will be owned by root.
61 # Doesn't happen on OS X but does on Linux. So we will do
62 # UID and GID for Linux only (this won't work on OS X anyway).
63 declare USER_FLAG=""
64 if test "Linux" == "$(uname || true)"
65 then
66 USER_FLAG="-u $(id -u):$(id -g)"
67 fi
68
69 printf "\n\n\n This will run in Docker. \n If you get an error, ensure Docker is installed. \n\n\n"
70 (
71 set -x
72 docker run \
73 -i \
74 ${TTY_FLAG} \
75 ${USER_FLAG} \
76 --rm \
77 -v "${ROOT_DIR}":"${ROOT_DIR}":cached \
78 -w "${ROOT_DIR}" \
79 "${IMAGE}" \
80 "${THIS_SCRIPT}"
81 )
82 exit 0
83 fi
84
85 function check_comments {
86 # no bombing out on errors with grep
87 set +e
88
89 # This is quite mad but don't fear the https://regex101.com/ helps a lot.
90 grep -Przo --color --include \*.go --exclude \*.pb.go --exclude bindata.go --exclude-dir vendor \
91 '\n.*\s+//(\s{0,3}[^\s^+][^\n]+[^.?!:]{2}|[^\s].*)\n[ \t]*[^/\s].*\n' ./
92 res=$?
93 set -e
94
95 # man grep: Normally, the exit status is 0 if selected lines are found and 1 otherwise.
96 # But the exit status is 2 if an error occurred, unless the -q or --quiet or --silent
97 # option is used and a selected line is found.
98 if test "0" == "${res}" # found something
99 then
100 printf "\n\n\n Error: Found comments without trailing period. Comments has to be full sentences.\n\n\n"
101 exit 1
102 elif test "1" == "${res}" # nothing found, all clear
103 then
104 printf "\n\n\n All comment formatting is good, Spartan.\n\n\n"
105 exit 0
106 else # grep error
107 printf "\n\n\n Hmmm something didn't work, issues with grep?.\n\n\n"
108 exit 2
109 fi
110 }
111
112 check_comments