fix: refactor golangci-lint scripts to improve version resolution and installation process
This commit is contained in:
@@ -4,70 +4,59 @@ set -euo pipefail
|
||||
# Wrapper script for golangci-lint fast linters in pre-commit
|
||||
# This ensures golangci-lint works in both terminal and VS Code pre-commit integration
|
||||
|
||||
# Find golangci-lint in common locations
|
||||
GOLANGCI_LINT=""
|
||||
preferred_bin="${GOBIN:-${GOPATH:-$HOME/go}/bin}/golangci-lint"
|
||||
|
||||
# Check if already in PATH
|
||||
if command -v golangci-lint >/dev/null 2>&1; then
|
||||
GOLANGCI_LINT="golangci-lint"
|
||||
else
|
||||
# Check common installation locations
|
||||
COMMON_PATHS=(
|
||||
lint_major_version() {
|
||||
local binary_path="$1"
|
||||
"$binary_path" version 2>/dev/null | sed -nE 's/.*version[[:space:]]+([0-9]+)\..*/\1/p' | sed -n '1p'
|
||||
}
|
||||
|
||||
install_v2_linter() {
|
||||
echo "🔧 Installing golangci-lint v2 with current Go toolchain..."
|
||||
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
|
||||
}
|
||||
|
||||
resolve_v2_linter() {
|
||||
local candidates=()
|
||||
local path_linter=""
|
||||
|
||||
if path_linter=$(command -v golangci-lint 2>/dev/null); then
|
||||
candidates+=("$path_linter")
|
||||
fi
|
||||
|
||||
candidates+=(
|
||||
"$preferred_bin"
|
||||
"$HOME/go/bin/golangci-lint"
|
||||
"/usr/local/bin/golangci-lint"
|
||||
"/usr/bin/golangci-lint"
|
||||
"${GOPATH:-$HOME/go}/bin/golangci-lint"
|
||||
)
|
||||
|
||||
for path in "${COMMON_PATHS[@]}"; do
|
||||
if [[ -x "$path" ]]; then
|
||||
GOLANGCI_LINT="$path"
|
||||
break
|
||||
for candidate in "${candidates[@]}"; do
|
||||
if [[ -x "$candidate" && "$(lint_major_version "$candidate")" == "2" ]]; then
|
||||
printf '%s\n' "$candidate"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Exit if not found
|
||||
if [[ -z "$GOLANGCI_LINT" ]]; then
|
||||
echo "ERROR: golangci-lint not found in PATH or common locations"
|
||||
echo "Searched:"
|
||||
echo " - PATH: $PATH"
|
||||
echo " - $HOME/go/bin/golangci-lint"
|
||||
echo " - /usr/local/bin/golangci-lint"
|
||||
echo " - /usr/bin/golangci-lint"
|
||||
echo ""
|
||||
echo "Install from: https://golangci-lint.run/usage/install/"
|
||||
install_v2_linter
|
||||
|
||||
if [[ -x "$preferred_bin" && "$(lint_major_version "$preferred_bin")" == "2" ]]; then
|
||||
printf '%s\n' "$preferred_bin"
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
if ! GOLANGCI_LINT="$(resolve_v2_linter)"; then
|
||||
echo "ERROR: failed to resolve golangci-lint v2"
|
||||
echo "PATH: $PATH"
|
||||
echo "Expected v2 binary at: $preferred_bin"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Version compatibility check
|
||||
# Extract Go versions from golangci-lint and system Go
|
||||
LINT_GO_VERSION=$("$GOLANGCI_LINT" version 2>/dev/null | grep -oP 'go\K[0-9]+\.[0-9]+(?:\.[0-9]+)?' || echo "")
|
||||
SYSTEM_GO_VERSION=$(go version 2>/dev/null | grep -oP 'go\K[0-9]+\.[0-9]+(?:\.[0-9]+)?' || echo "")
|
||||
|
||||
if [[ -n "$LINT_GO_VERSION" && -n "$SYSTEM_GO_VERSION" && "$LINT_GO_VERSION" != "$SYSTEM_GO_VERSION" ]]; then
|
||||
echo "⚠️ golangci-lint Go version mismatch detected:"
|
||||
echo " golangci-lint: $LINT_GO_VERSION"
|
||||
echo " system Go: $SYSTEM_GO_VERSION"
|
||||
echo ""
|
||||
echo "🔧 Auto-rebuilding golangci-lint with current Go version..."
|
||||
|
||||
if go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest 2>&1; then
|
||||
echo "✅ golangci-lint rebuilt successfully"
|
||||
|
||||
# Re-verify the tool is still accessible
|
||||
if command -v golangci-lint >/dev/null 2>&1; then
|
||||
GOLANGCI_LINT="golangci-lint"
|
||||
else
|
||||
GOLANGCI_LINT="$HOME/go/bin/golangci-lint"
|
||||
fi
|
||||
else
|
||||
echo "❌ Failed to rebuild golangci-lint"
|
||||
echo " Please run manually: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
|
||||
exit 1
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
echo "Using golangci-lint: $GOLANGCI_LINT"
|
||||
echo "Version: $($GOLANGCI_LINT version)"
|
||||
|
||||
# Change to backend directory and run golangci-lint
|
||||
cd "$(dirname "$0")/../../backend" || exit 1
|
||||
|
||||
Reference in New Issue
Block a user