diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..2a947a665f --- /dev/null +++ b/.pre-commit-config.yaml @@ -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/)" \ No newline at end of file diff --git a/Makefile b/Makefile index 8697bff79b..7c3a76f755 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/build-support/scripts/devtools.sh b/build-support/scripts/devtools.sh index b4cf687460..15e9401d7f 100755 --- a/build-support/scripts/devtools.sh +++ b/build-support/scripts/devtools.sh @@ -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 } @@ -51,6 +52,10 @@ function main { codegen_install return 0 ;; + -pre-commit ) + pre_commit_install + return 0 + ;; -h | --help ) usage return 0 @@ -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' @@ -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 { @@ -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