From f3581d100c18b4bfea4a17dcfb93c00a352778e0 Mon Sep 17 00:00:00 2001 From: xrstf Date: Thu, 16 May 2024 09:29:30 +0300 Subject: [PATCH] WIPW --- .github/workflows/go.yml | 6 +-- hack/verify.sh | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100755 hack/verify.sh diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fa99015..1e3872a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -8,7 +8,7 @@ on: branches: [main] jobs: - build: + verify: runs-on: ubuntu-latest steps: - name: Checkout @@ -19,5 +19,5 @@ jobs: with: go-version: '1.22.3' - - name: go mod tidy - uses: evantorrie/mott-the-tidier@v1-beta + - name: verify source + run: hack/verify.sh diff --git a/hack/verify.sh b/hack/verify.sh new file mode 100755 index 0000000..6ce57f3 --- /dev/null +++ b/hack/verify.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash + +set -euo pipefail +cd $(dirname $0)/.. + +EXIT_CODE=0 + +try() { + local title="$1" + shift + + echo "====================================" + echo "$title" + echo "====================================" + echo + + startTime=$(date +%s) + + set +e + $@ + exitCode=$? + set -e + + elapsed=$(($(date +%s) - $startTime)) + + if [[ $exitCode -eq 0 ]]; then + echo -e "\n[${elapsed}s] SUCCESS :)" + else + echo -e "\n[${elapsed}s] FAILED." + EXIT_CODE=1 + fi + + git reset --hard --quiet + git clean --force + + echo +} + +function verify_go_mod_tidy() { + (set -x; go mod tidy) + + if ! git diff --exit-code; then + echo "Please run go mod tidy." + return 1 + fi + + echo "go.mod is tidy." +} + +function verify_go_imports() { + (set -x; gimps .) + + if ! git diff --exit-code; then + echo "Some import statements are not properly grouped. Please run https://github.com/xrstf/gimps or sort them manually." + return 1 + fi + + echo "Go import statements are properly sorted." +} + +function verify_go_build() { + if ! (set -x; make build); then + echo "Code does not compile." + return 1 + fi + + echo "Code compiles." +} + +function verify_go_lint() { + if ! (set -x; golangci-lint run ./...); then + echo "Computer says no." + return 1 + fi + + echo "Code looks sane." +} + +try "go.mod tidy?" verify_go_mod_tidy +try "gimpsed?" verify_go_imports +try "Go code builds?" verify_go_build +try "Linting issues?" verify_go_lint + +exit $EXIT_CODE