Skip to content

Commit

Permalink
toil: use pre-commit to maintain properly formatted imports (#17940)
Browse files Browse the repository at this point in the history
pro-tip: you can skip this check with `git commit --no-verify` to override
  • Loading branch information
analogue authored Nov 9, 2023
1 parent f5bf256 commit 4273616
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
39 changes: 39 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/tekwizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
# Formats go imports into deterministic sections
# `pre-commit run gci` to run in isolation
- id: my-cmd
name: gci
# skip all generated go files
exclude: |
(?x)(
^proto-public/|
^proto/|
^agent/xds/z_xds_packages\.go$|
^testing/deployer/topology/default_versions\.go$|
\.deepcopy\.go$|
\.gen\.go$|
\.pb\.go$|
\.pb\.binary\.go$|
generated_funcs\.go$|
_generated_test\.go$|
mock_.+\.go$
)
args:
- "gci"
- "write"
- "--section"
- "standard"
- "--section"
- "default"
- "--section"
- "prefix(github.com/hashicorp/)"
- "--section"
- "prefix(github.com/hashicorp/consul/)"
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ GOLANGCI_LINT_VERSION='v1.51.1'
MOCKERY_VERSION='v2.20.0'
BUF_VERSION='v1.26.0'

PROTOC_GEN_GO_GRPC_VERSION="v1.2.0"
PROTOC_GEN_GO_GRPC_VERSION='v1.2.0'
MOG_VERSION='v0.4.1'
PROTOC_GO_INJECT_TAG_VERSION='v1.3.0'
PROTOC_GEN_GO_BINARY_VERSION="v0.1.0"
PROTOC_GEN_GO_BINARY_VERSION='v0.1.0'
DEEP_COPY_VERSION='bc3f5aa5735d8a54961580a3a24422c308c831c2'
COPYWRITE_TOOL_VERSION='v0.16.4'
# Go imports formatter
GCI_VERSION='v0.11.2'

MOCKED_PB_DIRS= pbdns

Expand Down
55 changes: 54 additions & 1 deletion build-support/scripts/devtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Options:
-protobuf Just install tools for protobuf.
-lint Just install tools for linting.
-codegen Just install tools for codegen.
-pre-commit Just install pre-commit.
-h | --help Print this help text.
EOF
}
Expand All @@ -51,6 +52,10 @@ function main {
codegen_install
return 0
;;
-pre-commit )
pre_commit_install
return 0
;;
-h | --help )
usage
return 0
Expand Down Expand Up @@ -139,6 +144,9 @@ function lint_install {
local golangci_lint_version
golangci_lint_version="$(make --no-print-directory print-GOLANGCI_LINT_VERSION)"

local gci_version
gci_version="$(make --no-print-directory print-GCI_VERSION)"

install_unversioned_tool \
'lint-consul-retry' \
'github.com/hashicorp/lint-consul-retry@master'
Expand All @@ -152,6 +160,12 @@ function lint_install {
'github.com/golangci/golangci-lint' \
"${golangci_lint_version}" \
'github.com/golangci/golangci-lint/cmd/golangci-lint'

install_versioned_tool \
'gci' \
'github.com/daixiang0/gci' \
"${gci_version}" \
'github.com/daixiang0/gci'
}

function codegen_install {
Expand Down Expand Up @@ -181,11 +195,50 @@ function copywrite_install {
'github.com/hashicorp/copywrite'
}

function tools_install {
function pre_commit_install {
# if already installed make sure the hook is also installed
if command -v "pre-commit" &>/dev/null; then
# Not to be confused with installing the tool, this installs
# the git hook locally (.git/hooks/pre-commit) which pre-commit
# uses as a vector to run checks on `git commit`. This hook is
# generated based on the local environment hence not source
# controlled.
pre-commit install
return 0
fi

# Install options based on https://pre-commit.com/#installation
if command -v "brew" &>/dev/null; then
brew install pre-commit && pre-commit install
return 0
fi

# Try python regardless of platform (mac, linux, etc)
if command -v "pip3" &>/dev/null; then
pip3 install pre-commit && pre-commit install
return 0
fi

# Can't disappoint the linux/debian folks
if command -v "apt" &>/dev/null; then
sudo apt-get install -yq pre-commit && pre-commit install
return 0
fi

if [[ "$(uname)" == "Darwin" ]]; then
echo "ERROR: Install homebrew from https://brew.sh/ so that pre-commit (https://pre-commit.com) can be installed."
return 1
fi

echo "ERROR: Install python3 and pip3 so that pre-commit (https://pre-commit.com) can be installed."
return 1
}

function tools_install {
lint_install
proto_tools_install
codegen_install
pre_commit_install
copywrite_install

return 0
Expand Down

0 comments on commit 4273616

Please sign in to comment.