Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ release: release-docker release-cli
#- Generate -#
#------------#

gen: install-hooks vendor pkg/bundle/deploy.go
gen: vendor pkg/bundle/deploy.go
@echo "✅ gen"
.PHONY: gen

Expand Down Expand Up @@ -184,7 +184,7 @@ test: lint test-go
.PHONY: test

lint: gen
@./scripts/run-golangci-lint.sh makefile
@./scripts/run-golangci-lint.sh
@echo "✅ lint"
.PHONY: lint

Expand Down Expand Up @@ -317,12 +317,3 @@ deepcopy-gen:
}
DEEPCOPY_GEN=$(GOBIN)/deepcopy-gen
.PHONY: deepcopy-gen

install-hooks:
@if [ -d .git ]; then \
if [ "$(shell git config core.hooksPath)" != ".githooks" ]; then \
git config core.hooksPath .githooks; \
echo "Git hooks path set to .githooks"; \
fi; \
fi
.PHONY: install-hooks
109 changes: 20 additions & 89 deletions scripts/run-golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh

# Unified golangci-lint runner script
# Usage: ./scripts/run-golangci-lint.sh <mode>
# Modes: precommit, makefile
# golangci-lint runner script
# Usage: ./scripts/run-golangci-lint.sh

# Uncomment this to see the commands being run
# Note: We don't use 'set -e' because we need to handle golangci-lint's
Expand All @@ -12,85 +11,30 @@
# Exit code used by golangci-lint when linting issues are found
# Allows us to distinguish between linting issues and other errors
readonly ISSUES_EXIT_CODE=42

MODE="${1}"

# Safely check whether Go is installed and set the GOPATH
# Also check for GOBIN and add it to the PATH in case the binary was installed to a non-standard location
if command -v go >/dev/null 2>&1; then
GOBIN="$(go env GOBIN)"
GOPATH_BIN="$(go env GOPATH)/bin"
[ -n "${GOBIN}" ] && export PATH="${PATH}:${GOBIN}"
export PATH="${PATH}:${GOPATH_BIN}"
fi

# Function to install golangci-lint (for makefile mode)
readonly GOLANGCI_LINT_VERSION=v2.15.0

# Safely check whether Go is installed and locate its binary directories
if command -v go >/dev/null 2>&1; then
GOBIN="$(go env GOBIN)"
GOPATH_BIN="$(go env GOPATH)"
GOPATH_BIN="${GOPATH_BIN%%:*}/bin"
# Prefer the installed linter, with GOBIN taking precedence when set
export PATH="${GOPATH_BIN}:${PATH}"
[ -n "${GOBIN}" ] && export PATH="${GOBIN}:${PATH}"
fi

# Function to install golangci-lint
install_golangci_lint() {
echo "Installing the latest golangci-lint with local toolchain"
if ! go install -a "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.5.0"
echo "Installing golangci-lint ${GOLANGCI_LINT_VERSION}"
if ! go install -a "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}"
then
echo "⚠️ Failed to install golangci-lint"
exit 0
fi
}

# Function to run golangci-lint in precommit mode
run_precommit_lint() {
echo "Running golangci-lint in precommit mode..."

# Check if golangci-lint is available - exit gracefully if it isn't
if ! command -v golangci-lint >/dev/null 2>&1
then
echo "⚠️ golangci-lint not found – run 'make lint' to install it automatically"
exit 0
fi

# Collect staged Go files as positional args
# cached: retrieve only staged files
# name-only: retrieve only the names of the files
# diff-filter=ACMR: retrieve only the files that have been added, copied, modified, or renamed
# grep -E '\.go$': retrieve only the files that end with .go
# || true: if there are no staged Go files, return a zero-status exit code
set -- $(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.go$' || true)
# If there are no staged Go files, exit successfully
if [ "$#" -eq 0 ]; then
echo "✅ No staged Go files were found, exiting"
exit 0
fi

echo "Running golangci-lint on staged files..."
BASE=$(git rev-parse --verify HEAD 2>/dev/null || echo "")

if [ -z "${BASE}" ]
then
# Initial commit – lint only staged files
golangci-lint run --issues-exit-code=${ISSUES_EXIT_CODE} --config .golangci.yml "${@}"
else
# Lint only staged changes vs HEAD
golangci-lint run --issues-exit-code=${ISSUES_EXIT_CODE} --config .golangci.yml --new-from-rev="${BASE}" "${@}"
fi

# Store the exit code
LINT_EXIT_CODE=${?}

# Handle exit codes properly
if [ ${LINT_EXIT_CODE} -eq ${ISSUES_EXIT_CODE} ]
then
echo "❌ golangci-lint found linting issues in staged files. Please fix them before committing."
exit 1
elif [ ${LINT_EXIT_CODE} -ne 0 ]
then
echo "⚠️ golangci-lint encountered an error (exit code: ${LINT_EXIT_CODE})"
exit 0
fi

echo "✅ golangci-lint passed!"
}

# Function to run golangci-lint in makefile mode
run_makefile_lint() {
echo "Running golangci-lint in makefile mode..."

# Function to run golangci-lint
run_lint() {
# Install golangci-lint if needed
install_golangci_lint

Expand All @@ -114,17 +58,4 @@ run_makefile_lint() {
echo "✅ golangci-lint passed!"
}

# Main logic
case "${MODE}" in
"precommit")
run_precommit_lint
;;
"makefile")
run_makefile_lint
;;
*)
echo "Usage: ${0} <mode>"
echo "Modes: precommit, makefile"
exit 1
;;
esac
run_lint
Loading