Skip to content

Commit

Permalink
chore: add go pre-commit checks (#7056)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidzr authored Jun 21, 2023
1 parent deb22ff commit 90fc15a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ repos:
entry: ./pre-commit/check.py
require_serial: true
language: python
# golang hooks based on https://github.com/Bahjat/pre-commit-golang
- id: go-fmt-import
name: "go import/fmt"
entry: ./pre-commit/run-go-fmt-import.sh
files: '\.go$'
language: "script"
description: "Runs `goimport,gofmt`, requires golang"
exclude: ^(proto/pkg)
- id: gofumpt
name: "gofumpt"
entry: ./pre-commit/run-go-gofumpt.sh
files: '\.go$'
language: "script"
description: "Runs `gofumpt`, requires github.com/mvdan/gofumpt"
exclude: ^(proto/pkg)
- id: golangci-lint
name: "go golangci-lint"
entry: ./pre-commit/run-golangci-lint.sh
files: '\.go$'
language: "script"
description: "Runs `golangci-lint`, install https://github.com/golangci/golangci-lint"
exclude: ^(proto/pkg)

- repo: https://github.com/adrienverge/yamllint.git
rev: v1.28.0
Expand Down
8 changes: 8 additions & 0 deletions pre-commit/run-go-fmt-import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

LIST_OF_FILES=$(goimports -l -w "$@")
# print a list of affected files if any
echo "$LIST_OF_FILES"
if [ -n "$LIST_OF_FILES" ]; then
exit 1
fi
8 changes: 8 additions & 0 deletions pre-commit/run-go-gofumpt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

LIST_OF_FILES=$(gofumpt -l -w "$@")
# print a list of affected files if any
echo "$LIST_OF_FILES"
if [ -n "$LIST_OF_FILES" ]; then
exit 1
fi
16 changes: 16 additions & 0 deletions pre-commit/run-golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

CONFIG_FILE="$1"

if ! command -v golangci-lint /dev/null 2>&1; then
echo "golangci-lint could not be found"
exit
fi

ERRS=$(golangci-lint run --new-from-rev="$(git rev-parse HEAD)" "$CONFIG_FILE")

if [ -n "${ERRS}" ]; then
echo "${ERRS}"
exit 1
fi
exit 0

0 comments on commit 90fc15a

Please sign in to comment.