Skip to content

Commit

Permalink
Adds pre-commit to improve quality (#184)
Browse files Browse the repository at this point in the history
* Adds initial hooks

* Work on tool install

* Actually implements the hooks
Does the actual hook implementation but doesn't run them.

* --no-verify

improve readme with --no-verify tip

* RUNS commits hooks

* Revert "RUNS commits hooks"

This reverts commit 30100f1.

* Removes tomlv from tools

* Updates RM instructions

* Update README.md

Co-authored-by: Daniel <[email protected]>
  • Loading branch information
danwt and Daniel authored Jul 11, 2022
1 parent 9f329d8 commit f3c8c3c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# install pre-commit `brew install pre-commit`
# install hooks to git `pre-commit install`
# update the hooks `pre-commit autoupdate`
# to run the hooks `pre-commit run --all-files`
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-merge-conflict
- id: check-added-large-files
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: local
hooks:
- id: install-tools
name: "install tools"
entry: "./scripts/install_tools.sh"
language: script
description: "Installs tools needed for go static analysis hooks"
- id: go-vet
name: "go vet"
entry: "./scripts/go_vet.sh"
language: script
description: "Installs tools needed for go static analysis hooks"

- repo: https://github.com/dnephin/pre-commit-golang
rev: master
hooks:
- id: go-fmt
- id: go-imports
- id: go-cyclo
args: [-over=15]
- id: go-mod-tidy
- id: go-build
- id: go-critic
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ gofmt -w -s -e .
go vet ./...
```

Some useful tools are included in the repository using [pre-commit](https://pre-commit.com/hooks.html). pre-commit lets you run developer tools either on every git commit, or manually with `pre-commit run --all-files`. See the [config](./.pre-commit-config.yaml) for details. In this repo the hooks are not installed to git, as that can be cumbersome, but it is still possible to benefit from them.

```bash
## Prerequisites

# pre-commit
brew install pre-commit
# goimports (https://pkg.go.dev/golang.org/x/tools/cmd/goimports)
go install golang.org/x/tools/cmd/goimports@latest
# gocyclo (https://github.com/fzipp/gocyclo)
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
# go-critic https://github.com/go-critic/go-critic
go install github.com/go-critic/go-critic/cmd/gocritic@latest

## Run the tools

pre-commit run --all-files
```

**Debugging**

If using VSCode, see [vscode-go/wiki/debugging](https://github.com/golang/vscode-go/wiki/debugging) to debug unit tests or go binaries.
Expand Down
7 changes: 7 additions & 0 deletions scripts/go_vet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -e
pkg=$(go list ./...)
for dir in $(echo $@|xargs -n1 dirname|sort -u); do
go vet $pkg/$dir
done
20 changes: 20 additions & 0 deletions scripts/install_tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

set -e -o pipefail

if ! command -v gocyclo &> /dev/null ; then
echo "gocyclo not installed or available in the PATH, installing" >&2
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest;
fi

if ! command -v gocritic &> /dev/null ; then
echo "gocritic not installed or available in the PATH, installing" >&2
go install github.com/go-critic/go-critic/cmd/gocritic@latest;
fi

if ! command -v goimports &> /dev/null ; then
echo "goimports not installed or available in the PATH, installing" >&2
go install golang.org/x/tools/cmd/goimports@latest;
fi

exit 0

0 comments on commit f3c8c3c

Please sign in to comment.