一键导入
git-hooks
Configure automated git hooks using lefthook with Go project best practices including linting, testing, and coverage requirements.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Configure automated git hooks using lefthook with Go project best practices including linting, testing, and coverage requirements.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
fraudctl-specific patterns including KNN vector search optimization, 14D vectorization, zero-allocation patterns, and performance best practices for high-throughput fraud detection APIs.
Go performance profiling patterns using pprof, benchmarks, and optimization strategies for identifying and fixing performance bottlenecks.
Prevents destructive operations by requiring confirmation and verification before executing risky commands.
Idiomatic Go patterns, best practices, and conventions for building robust, efficient, and maintainable Go applications.
Go testing patterns including table-driven tests, subtests, benchmarks, fuzzing, and test coverage. Follows TDD methodology with idiomatic Go practices.
| name | git-hooks |
| description | Configure automated git hooks using lefthook with Go project best practices including linting, testing, and coverage requirements. |
| origin | fraudctl |
This skill provides automated git hooks using lefthook to ensure code quality and prevent bad commits from entering the repository.
# lefthook.yml
# https://github.com/evilmartians/lefthook
pre-commit:
parallel: true
commands:
fmt:
glob: "**/*.go"
exclude:
- '^\.agents/.*'
- '^bin/.*'
run: bash -c 'files=$(gofmt -l .); [ -z "$files" ] && exit 0; printf "unformatted files:\n%s\n" "$files"; exit 1'
vet:
glob: "**/*.go"
exclude:
- '^\.agents/.*'
run: go vet ./...
lint:
glob: "**/*.go"
exclude:
- '^\.agents/.*'
run: bash -c 'command -v golangci-lint >/dev/null 2>&1 || exit 0; golangci-lint run'
test:
exclude:
- '^\.agents/.*'
- '^bin/.*'
run: |
go test -race -coverprofile=/tmp/coverage.out ./... &&
COV=$(go tool cover -func=/tmp/coverage.out | grep "^total:" | awk '{gsub(/%/,"",$3); print $3}') &&
awk -v cov="$COV" "BEGIN {if (cov+0 < 80) {print \"FAIL: Coverage \" cov \"%\"; exit 1}}"
pre-push:
parallel: true
commands:
build:
exclude:
- '^\.agents/.*'
run: go build ./...
lint-full:
exclude:
- '^\.agents/.*'
run: bash -c 'command -v golangci-lint >/dev/null 2>&1 || exit 0; golangci-lint run --timeout=5m'
test-cover:
exclude:
- '^\.agents/.*'
- '^bin/.*'
run: |
go test -race -coverprofile=/tmp/coverage.out ./... &&
COV=$(go tool cover -func=/tmp/coverage.out | grep "^total:" | awk '{gsub(/%/,"",$3); print $3}') &&
awk -v cov="$COV" "BEGIN {if (cov+0 < 80) {print \"FAIL: Coverage \" cov \"%\"; exit 1}}"
vuln:
exclude:
- '^\.agents/.*'
run: bash -c 'command -v govulncheck >/dev/null 2>&1 || exit 0; govulncheck ./...'
The exclude option in lefthook is for filtering files passed to commands, not for glob patterns. Each item must be a regex pattern:
# CORRECT - list of regex patterns
exclude:
- '^\.agents/.*'
- '^bin/.*'
# WRONG - trying to use anchor as exclude (causes YAML error)
_standard_excludes: &standard_excludes
- '^\.agents/'
For reliable coverage extraction, use awk -v to pass the coverage value:
# This is reliable
COV=$(go tool cover -func=coverage.out | grep "^total:" | awk '{gsub(/%/,"",$3); print $3}')
# This can fail with special characters
COV=$(go tool cover -func=coverage.out | grep '^total:' | awk '{print $3}' | tr -d '%')
# Install lefthook
npm install -g lefthook
# Or with Go
go install github.com/evilmartians/lefthook/cmd/lefthook@latest
# Initialize in project
lefthook install
# Run hooks manually
lefthook run pre-commit
lefthook run pre-commit --force # Force run all hooks
# Run specific hook
lefthook run lint
| Hook | Purpose | Tool |
|---|---|---|
| fmt | Check formatting | gofmt |
| goimports | Check import order | goimports |
| vet | Static analysis | go vet |
| lint | Quick lint check | golangci-lint |
| test | Run tests with coverage | go test |
| Hook | Purpose | Tool |
|---|---|---|
| build | Verify build compiles | go build |
| lint-full | Full lint check | golangci-lint |
| test-cover | Verify 80% coverage | go test |
| vuln | Check vulnerabilities | govulncheck |
Go projects should maintain minimum 80% test coverage:
# Check coverage
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out | grep '^total:'
# Coverage by package
go test -coverprofile=coverage.out ./... -covermode=atomic
These tools are optional — hooks skip if not installed:
# Install golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# Install goimports
go install golang.org/x/tools/cmd/goimports@latest
# Install govulncheck
go install golang.org/x/vuln/cmd/govulncheck@latest
# Install semgrep
npm install -g semgrep
Hooks should fail fast to prevent bad commits:
# Example: Fail on any formatting issues
gofmt -l . && echo "Unformatted files found" && exit 1
# Example: Fail if coverage drops
COV=$(go tool cover -func=coverage.out | grep "^total:" | awk '{gsub(/%/,"",$3); print $3}')
if [ "$COV" -lt 80 ]; then
echo "Coverage $COV% is below 80%"
exit 1
fi
DO:
parallel: true for speed|| exit 0DON'T:
exclude (not supported)