wormhole/scripts/lint.sh

156 lines
4.3 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# fail if any command fails
set -eo pipefail -o nounset
ROOT="$(dirname "$(dirname "$(realpath "$0")")")"
2022-12-21 05:44:57 -08:00
DOCKERFILE="$ROOT/scripts/Dockerfile.lint"
VALID_COMMANDS=("lint" "format")
SELF_ARGS_WITHOUT_DOCKER=""
GOIMPORTS_ARGS=""
GOLANGCI_LINT_ARGS=""
print_help() {
cat <<-EOF >&2
Usage: $(basename "$0") [-h] [-c] [-w] [-d] [-l] COMMAND
COMMAND can be one of: "${VALID_COMMANDS[*]}"
-h Print this help.
-c Run in docker and don't worry about dependencies
-w Automatically fix all formatting issues
-d Print diff for all formatting issues
-l List files that have formatting issues
-g Format output to be parsed by github actions
EOF
}
format(){
if [ "$GOIMPORTS_ARGS" == "" ]; then
GOIMPORTS_ARGS="-l"
fi
# only -l supports output as github action
if [ "$GITHUB_ACTION" == "true" ]; then
GOIMPORTS_ARGS="-l"
fi
# Check for dependencies
if ! command -v goimports >/dev/null 2>&1; then
printf "%s\n" "Require goimports. You can run this command in a docker container instead with '-c' and not worry about it or install it: \n\tgo install golang.org/x/tools/cmd/goimports@latest" >&2
exit 1
fi
# Use -exec because of pitfall #1 in http://mywiki.wooledge.org/BashPitfalls
2022-10-27 20:19:17 -07:00
GOFMT_OUTPUT="$(find "./sdk" "./node" "./wormchain" -type f -name '*.go' -not -path '*.pb.go' -print0 | xargs -r -0 goimports $GOIMPORTS_ARGS 2>&1)"
if [ -n "$GOFMT_OUTPUT" ]; then
if [ "$GITHUB_ACTION" == "true" ]; then
GOFMT_OUTPUT="$(echo "$GOFMT_OUTPUT" | awk '{print "::error file="$0"::Formatting error. Please format using ./scripts/lint.sh -d format."}')"
fi
echo "$GOFMT_OUTPUT" >&2
exit 1
fi
}
lint(){
# Check for dependencies
if ! command -v golangci-lint >/dev/null 2>&1; then
printf "%s\n" "Require golangci-lint. You can run this command in a docker container instead with '-c' and not worry about it or install it: https://golangci-lint.run/usage/install/"
fi
# Do the actual linting!
cd "$ROOT"/node
node: upgrade to go 1.21.9 (#3855) * node: update quic-go Ran via: go get github.com/quic-go/quic-go@v0.42.0 * node: update mongo-drive dependency Ran via: go get go.mongodb.org/mongo-driver@latest * node: upgrade libp2p-go Ran via: go get github.com/libp2p/go-libp2p@v0.33.1 Refs: #3863 * node: fallout from the go upgrade in the go.sum Run via: go mod tidy This updates the go.sum and removes unnecessary indirect references. * docs: use go1.21.8 * github: use go1.21.8 Except for wormchain. * node: update dockerfiles to use go 1.21.8 This was done with scripts/update-go-version.sh * scripts: update linter dockerfile to use go 1.21.8 This was done with scripts/update-go-version.sh * wormchain: update ibc-relayer to use go 1.21.8 This was done with scripts/update-go-version.sh * scripts: add update-go-version.sh This is a little helper to make updating the version of go a bit nicer. * scripts: update-go-version.sh fixes * set the default docker command to "dokcer" * update the comment for the humongous sed command for dockerfiles * github: fix go linting * Upgrade golangci-lint to a version built with go 1.21.x. The older version was a binary version built with go 1.20.x and it was failing against the newer code built with go 1.21.x * print the golangci-lint version in each run to see what version of go it was built with in case there are incompatibilties during the next upgrade * remove the linter config skipping over pkg/supervisor entirely and instead put in an override to ignore the `unused` linter for the pkg/supervisor testhelpers bits for unsed test functions necessary to satisfy the test interface. * scripts: update golang linter cli invocation * github: use the latest version of golangci-lint Because 1.52.2 is built with go 1.20.x which has issues with this project now that it is upgraded to 1.21.8. * node: remove implicit memory aliasing in a loop Caught by an upgraded golangci-lint with the gosec linter: ::medium file=node/pkg/watchers/evm/connectors/batch_poller.go,line=226,col=8::G601: Implicit memory aliasing in for loop. (gosec) ::medium file=node/pkg/watchers/evm/connectors/batch_poller.go,line=285,col=8::G601: Implicit memory aliasing in for loop. (gosec) ::medium file=node/pkg/watchers/evm/connectors/batch_poller_test.go,line=128,col=37::G601: Implicit memory aliasing in for loop. (gosec) See also: https://husni.dev/beware-of-implicit-memory-aliasing-in-go-foor-loop/ * node: update logging * Update go to 1.21.9 Automated via: scripts/update-go-version.sh 1.21.9 * node: update test root context teardown time Otherwise things get really sad. * scripts: additions to update-go-versions.sh * Standardized on prefacing functions with `function` for consistency * Added a few more comments to explain how things work * Automatically increment the go version and toolchain in go.mod * Standardized on prefacing functions with `function` for consistency * Make the go image debian version a variable for ease of maintenance * node: update go.mod to specify 1.21.9 * node: update node tests Review feedback from @pires * scripts: run go mod tidy in update-go-version.sh Required after running go mod edit or it refuses to build. * node: update go.mod Running `go mod tidy` removes the toolchain so the build works. * node: update node tests --------- Co-authored-by: Ryan Hamphrey <hamphreyryan26@gmail.com>
2024-04-19 11:43:36 -07:00
golangci-lint run --timeout=10m --path-prefix=node $GOLANGCI_LINT_ARGS ./...
2022-08-30 00:51:36 -07:00
cd "${ROOT}/sdk"
golangci-lint run --timeout=10m $GOLANGCI_LINT_ARGS ./...
}
DOCKER="false"
GITHUB_ACTION="false"
while getopts 'cwdlgh' opt; do
case "$opt" in
c)
DOCKER="true"
;;
w)
GOIMPORTS_ARGS+="-w "
SELF_ARGS_WITHOUT_DOCKER+="-w "
;;
d)
GOIMPORTS_ARGS+="-d "
SELF_ARGS_WITHOUT_DOCKER+="-d "
;;
l)
GOIMPORTS_ARGS+="-l "
SELF_ARGS_WITHOUT_DOCKER+="-l "
;;
g)
GOLANGCI_LINT_ARGS+="--out-format=github-actions "
GITHUB_ACTION="true"
SELF_ARGS_WITHOUT_DOCKER+="-g "
;;
h)
print_help
exit 0
;;
?)
echo "Invalid command option." >&2
print_help
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ "$#" -ne "1" ]; then
echo "Need to specify COMMAND." >&2
print_help
exit 1
fi
COMMAND="$1"
if [[ ! " ${VALID_COMMANDS[*]} " == *" $COMMAND "* ]]; then
echo "Invalid command $COMMAND." >&2
print_help
exit 1
fi
# run this script recursively inside docker, if requested
if [ "$DOCKER" == "true" ]; then
# The easy thing to do here would be to use a bind mount to share the code with the container.
# But this doesn't work in scenarios where we are in a container already.
# But it's easy so we just won't support that case for now.
# If we wanted to support it, my idea would be to `docker run`, `docker cp`, `docker exec`, `docker rm`.
if grep -Esq 'docker|lxc|kubepods' /proc/1/cgroup; then
echo "Already running inside a container. This situation isn't supported (yet)." >&2
exit 1
fi
DOCKER_IMAGE="$(docker build -q -f "$DOCKERFILE" .)"
DOCKER_EXEC="./scripts/$(basename "$0")"
MOUNT="--mount=type=bind,target=/app,source=$PWD"
# for safety, mount as readonly unless -w flag was given
if ! [[ "$GOIMPORTS_ARGS" =~ "w" ]]; then
MOUNT+=",readonly"
fi
docker run --workdir /app "$MOUNT" "$DOCKER_IMAGE" "$DOCKER_EXEC" $SELF_ARGS_WITHOUT_DOCKER "$COMMAND"
exit "$?"
fi
case $COMMAND in
"lint")
lint
;;
"format")
format
;;
esac