toil: use pre-commit to maintain properly formatted imports (#17940)

pro-tip: you can skip this check with `git commit --no-verify` to override
This commit is contained in:
Semir Patel 2023-11-09 12:34:31 -06:00 committed by GitHub
parent f5bf256425
commit 4273616313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 3 deletions

39
.pre-commit-config.yaml Normal file
View File

@ -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/)"

View File

@ -14,12 +14,14 @@ GOLANGCI_LINT_VERSION='v1.51.1'
MOCKERY_VERSION='v2.20.0' MOCKERY_VERSION='v2.20.0'
BUF_VERSION='v1.26.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' MOG_VERSION='v0.4.1'
PROTOC_GO_INJECT_TAG_VERSION='v1.3.0' 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' DEEP_COPY_VERSION='bc3f5aa5735d8a54961580a3a24422c308c831c2'
COPYWRITE_TOOL_VERSION='v0.16.4' COPYWRITE_TOOL_VERSION='v0.16.4'
# Go imports formatter
GCI_VERSION='v0.11.2'
MOCKED_PB_DIRS= pbdns MOCKED_PB_DIRS= pbdns

View File

@ -25,6 +25,7 @@ Options:
-protobuf Just install tools for protobuf. -protobuf Just install tools for protobuf.
-lint Just install tools for linting. -lint Just install tools for linting.
-codegen Just install tools for codegen. -codegen Just install tools for codegen.
-pre-commit Just install pre-commit.
-h | --help Print this help text. -h | --help Print this help text.
EOF EOF
} }
@ -51,6 +52,10 @@ function main {
codegen_install codegen_install
return 0 return 0
;; ;;
-pre-commit )
pre_commit_install
return 0
;;
-h | --help ) -h | --help )
usage usage
return 0 return 0
@ -139,6 +144,9 @@ function lint_install {
local golangci_lint_version local golangci_lint_version
golangci_lint_version="$(make --no-print-directory print-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 \ install_unversioned_tool \
'lint-consul-retry' \ 'lint-consul-retry' \
'github.com/hashicorp/lint-consul-retry@master' 'github.com/hashicorp/lint-consul-retry@master'
@ -152,6 +160,12 @@ function lint_install {
'github.com/golangci/golangci-lint' \ 'github.com/golangci/golangci-lint' \
"${golangci_lint_version}" \ "${golangci_lint_version}" \
'github.com/golangci/golangci-lint/cmd/golangci-lint' '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 { function codegen_install {
@ -181,11 +195,50 @@ function copywrite_install {
'github.com/hashicorp/copywrite' '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 lint_install
proto_tools_install proto_tools_install
codegen_install codegen_install
pre_commit_install
copywrite_install copywrite_install
return 0 return 0