Skip to content

Instantly share code, notes, and snippets.

@maciej
Created June 26, 2025 12:57
Show Gist options
  • Save maciej/666679a34f755943ffa43d5c3c2854d6 to your computer and use it in GitHub Desktop.
Save maciej/666679a34f755943ffa43d5c3c2854d6 to your computer and use it in GitHub Desktop.
golangci-lint setup script
#!/usr/bin/env bash
set -euo pipefail
# Step 1: Extract the last element of the module name from go.mod
if [[ ! -f "go.mod" ]]; then
echo "Error: go.mod not found in current directory." >&2
exit 1
fi
module_name=$(awk '/^module / {print $2}' go.mod)
if [[ -z "$module_name" ]]; then
echo "Error: Unable to extract module name." >&2
exit 1
fi
# Step 2: Create tools directory if missing
mkdir -p tools
# Step 3: Create tools/golangci-lint.mod if it doesn't exist
modfile="tools/golangci-lint.mod"
if [[ ! -f "$modfile" ]]; then
go mod init -modfile=tools/golangci-lint.mod "${module_name}/tools/golangci-lint"
fi
# Step 4: Add golangci-lint
go get -tool -modfile="$modfile" github.com/golangci/golangci-lint/v2/cmd/[email protected]
# Step 5: Tidy the tools module
go mod tidy -modfile="$modfile"
echo "golangci-lint tool module setup complete."
@maciej
Copy link
Author

maciej commented Jun 26, 2025

Assuming you use ~/.local/bin; install by:

curl -fsSL https://gist.githubusercontent.com/maciej/666679a34f755943ffa43d5c3c2854d6/raw/6637cd2ef73aec6fc0ef2e7d78b7d7610ff42067/golangci_lint_setup.sh -o ~/.local/bin/golangci_lint_setup.sh && chmod +x ~/.local/bin/golangci_lint_setup.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment