mirror of
https://github.com/weaveworks/scope.git
synced 2026-09-06 10:17:19 +00:00
4b7d5c61 Merge pull request #59 from weaveworks/57-fix-lint-properly b7f0e692 Merge pull request #58 from weaveworks/fix-lint 794702c7 Pin version of shfmt ab1b11de Fix lint 81d80f35 Merge pull request #55 from weaveworks/lint-tf 05ad5f27 Review feedback 4c0d0469 Use hclfmt to lint terraform. fd875e27 Fix test wrt shellcheck 54ec2d92 Don't capitalise error messages 19d3b6e2 Merge pull request #49 from weaveworks/pin-shfmt fea98f66 Go get from the vendor dir 1d867b06 Try and vendor a specific version of shfmt 76619c2d Merge pull request #48 from weaveworks/revert-41-user-tokens 4f96c519 Revert "Add experimental support for user tokens" d00033fd Merge pull request #41 from weaveworks/user-tokens 245ed267 Merge pull request #47 from weaveworks/46-shfmt c1d7815a Fix shfmt error cb397466 Don't overright lint_result with 0 when shellcheck succeeds 8ab80e87 Merge pull request #45 from weaveworks/lint 83d5bd1f getting integration/config and test shellcheck-compliant cff9ec36 Fix some shellcheck errors 7a843d6d run shellcheck as part of lint if it is installed 31552a0e removing spurious space from test 6ca7c5f0 Merge pull request #44 from weaveworks/shfmt 952356d8 Allow lint to lint itself b7ac59c3 Run shfmt on all shell files in this repo 5570b0e9 Add shfmt formatting of shell files in lint 0a675941 fix circle build by splatting gopath permissions 354e0838 Fixing lint 586060b2 Add experimental support for user tokens git-subtree-dir: tools git-subtree-split: 4b7d5c617e662acb8b1bee4203d7671fb0aa1cba
214 lines
5.2 KiB
Bash
Executable File
214 lines
5.2 KiB
Bash
Executable File
#!/bin/bash
|
|
# This scipt lints files for common errors.
|
|
#
|
|
# For go files, it runs gofmt and go vet, and optionally golint and
|
|
# gocyclo, if they are installed.
|
|
#
|
|
# For shell files, it runs shfmt. If you don't have that installed, you can get
|
|
# it with:
|
|
# go get -u github.com/mvdan/sh/cmd/shfmt
|
|
#
|
|
# With no arguments, it lints the current files staged
|
|
# for git commit. Or you can pass it explicit filenames
|
|
# (or directories) and it will lint them.
|
|
#
|
|
# To use this script automatically, run:
|
|
# ln -s ../../bin/lint .git/hooks/pre-commit
|
|
|
|
set -e
|
|
|
|
IGNORE_LINT_COMMENT=
|
|
IGNORE_TEST_PACKAGES=
|
|
IGNORE_SPELLINGS=
|
|
while true; do
|
|
case "$1" in
|
|
-nocomment)
|
|
IGNORE_LINT_COMMENT=1
|
|
shift 1
|
|
;;
|
|
-notestpackage)
|
|
IGNORE_TEST_PACKAGES=1
|
|
shift 1
|
|
;;
|
|
-ignorespelling)
|
|
IGNORE_SPELLINGS="$2,$IGNORE_SPELLINGS"
|
|
shift 2
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
spell_check() {
|
|
local filename="$1"
|
|
local lint_result=0
|
|
|
|
# we don't want to spell check tar balls, binaries, Makefile and json files
|
|
if file "$filename" | grep executable >/dev/null 2>&1; then
|
|
return $lint_result
|
|
fi
|
|
if [[ $filename == *".tar" || $filename == *".gz" || $filename == *".json" || $(basename "$filename") == "Makefile" ]]; then
|
|
return $lint_result
|
|
fi
|
|
|
|
# misspell is completely optional. If you don't like it
|
|
# don't have it installed.
|
|
if ! type misspell >/dev/null 2>&1; then
|
|
return $lint_result
|
|
fi
|
|
|
|
if ! misspell -error -i "$IGNORE_SPELLINGS" "${filename}"; then
|
|
lint_result=1
|
|
fi
|
|
|
|
return $lint_result
|
|
}
|
|
|
|
test_mismatch() {
|
|
local filename="$1"
|
|
local package=$(grep '^package ' "$filename" | awk '{print $2}')
|
|
local lint_result=0
|
|
|
|
if [[ $package == "main" ]]; then
|
|
return # in package main, all bets are off
|
|
fi
|
|
|
|
if [[ $filename == *"_internal_test.go" ]]; then
|
|
if [[ $package == *"_test" ]]; then
|
|
lint_result=1
|
|
echo "${filename}: should not be part of a _test package"
|
|
fi
|
|
else
|
|
if [[ ! $package == *"_test" ]]; then
|
|
lint_result=1
|
|
echo "${filename}: should be part of a _test package"
|
|
fi
|
|
fi
|
|
|
|
return $lint_result
|
|
}
|
|
|
|
lint_go() {
|
|
local filename="$1"
|
|
local lint_result=0
|
|
|
|
if [ -n "$(gofmt -s -l "${filename}")" ]; then
|
|
lint_result=1
|
|
echo "${filename}: run gofmt -s -w ${filename}"
|
|
fi
|
|
|
|
go tool vet "${filename}" || lint_result=$?
|
|
|
|
# golint is completely optional. If you don't like it
|
|
# don't have it installed.
|
|
if type golint >/dev/null 2>&1; then
|
|
# golint doesn't set an exit code it seems
|
|
if [ -z "$IGNORE_LINT_COMMENT" ]; then
|
|
lintoutput=$(golint "${filename}")
|
|
else
|
|
lintoutput=$(golint "${filename}" | grep -vE 'comment|dot imports|ALL_CAPS')
|
|
fi
|
|
if [ -n "$lintoutput" ]; then
|
|
lint_result=1
|
|
echo "$lintoutput"
|
|
fi
|
|
fi
|
|
|
|
# gocyclo is completely optional. If you don't like it
|
|
# don't have it installed. Also never blocks a commit,
|
|
# it just warns.
|
|
if type gocyclo >/dev/null 2>&1; then
|
|
gocyclo -over 25 "${filename}" | while read -r line; do
|
|
echo "${filename}": higher than 25 cyclomatic complexity - "${line}"
|
|
done
|
|
fi
|
|
|
|
return $lint_result
|
|
}
|
|
|
|
lint_sh() {
|
|
local filename="$1"
|
|
local lint_result=0
|
|
|
|
if ! diff <(shfmt -i 4 "${filename}") "${filename}" >/dev/null; then
|
|
lint_result=1
|
|
echo "${filename}: run shfmt -i 4 -w ${filename}"
|
|
fi
|
|
|
|
# the shellcheck is completely optional. If you don't like it
|
|
# don't have it installed.
|
|
if type shellcheck >/dev/null 2>&1; then
|
|
shellcheck "${filename}" || lint_result=1
|
|
fi
|
|
|
|
return $lint_result
|
|
}
|
|
|
|
lint_tf() {
|
|
local filename="$1"
|
|
local lint_result=0
|
|
|
|
if ! diff <(hclfmt "${filename}") "${filename}" >/dev/null; then
|
|
lint_result=1
|
|
echo "${filename}: run hclfmt -w ${filename}"
|
|
fi
|
|
|
|
return $lint_result
|
|
}
|
|
|
|
lint() {
|
|
filename="$1"
|
|
ext="${filename##*\.}"
|
|
local lint_result=0
|
|
|
|
# Don't lint deleted files
|
|
if [ ! -f "$filename" ]; then
|
|
return
|
|
fi
|
|
|
|
# Don't lint static.go
|
|
case "$(basename "${filename}")" in
|
|
static.go) return ;;
|
|
coverage.html) return ;;
|
|
esac
|
|
|
|
if [[ "$(file --mime-type "${filename}" | awk '{print $2}')" == "text/x-shellscript" ]]; then
|
|
ext="sh"
|
|
fi
|
|
|
|
case "$ext" in
|
|
go) lint_go "${filename}" || lint_result=1 ;;
|
|
sh) lint_sh "${filename}" || lint_result=1 ;;
|
|
tf) lint_tf "${filename}" || lint_result=1 ;;
|
|
esac
|
|
|
|
if [ -z "$IGNORE_TEST_PACKAGES" ]; then
|
|
if [[ "$filename" == *"_test.go" ]]; then
|
|
test_mismatch "${filename}" || lint_result=1
|
|
fi
|
|
fi
|
|
|
|
spell_check "${filename}" || lint_result=1
|
|
|
|
return $lint_result
|
|
}
|
|
|
|
lint_files() {
|
|
local lint_result=0
|
|
while read -r filename; do
|
|
lint "${filename}" || lint_result=1
|
|
done
|
|
exit $lint_result
|
|
}
|
|
|
|
list_files() {
|
|
if [ $# -gt 0 ]; then
|
|
git ls-files --exclude-standard | grep -vE '(^|/)vendor/'
|
|
else
|
|
git diff --cached --name-only
|
|
fi
|
|
}
|
|
|
|
list_files "$@" | lint_files
|