mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Add goimports as a check to the Github commit actions (#31432)
* Add goimports as a check to the Github commit actions * check diff against main instead of last commit * goimports should fix the problems * fix up conditionals and wildcard matching * specify origin/main in diff * fetch main branch when checkout * back to origin main
This commit is contained in:
parent
7b4a5513a9
commit
afd273d636
8
.github/workflows/checks.yml
vendored
8
.github/workflows/checks.yml
vendored
@ -141,6 +141,8 @@ jobs:
|
||||
steps:
|
||||
- name: "Fetch source code"
|
||||
uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0 # We need to do comparisons against the main branch.
|
||||
|
||||
- name: Determine Go version
|
||||
id: go
|
||||
@ -178,9 +180,13 @@ jobs:
|
||||
restore-keys: |
|
||||
protobuf-tools-
|
||||
|
||||
- name: Install CI tooling
|
||||
run: |
|
||||
go install golang.org/x/tools/cmd/goimports@v0.1.11
|
||||
|
||||
- name: "Code consistency checks"
|
||||
run: |
|
||||
make fmtcheck generate staticcheck exhaustive protobuf
|
||||
make fmtcheck importscheck generate staticcheck exhaustive protobuf
|
||||
if [[ -n "$(git status --porcelain)" ]]; then
|
||||
echo >&2 "ERROR: Generated files are inconsistent. Run 'make generate' and 'make protobuf' locally and then commit the updated files."
|
||||
git >&2 status --porcelain
|
||||
|
5
Makefile
5
Makefile
@ -35,6 +35,9 @@ protobuf:
|
||||
fmtcheck:
|
||||
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
|
||||
|
||||
importscheck:
|
||||
@sh -c "'$(CURDIR)/scripts/goimportscheck.sh'"
|
||||
|
||||
staticcheck:
|
||||
@sh -c "'$(CURDIR)/scripts/staticcheck.sh'"
|
||||
|
||||
@ -63,4 +66,4 @@ website/build-local:
|
||||
# under parallel conditions.
|
||||
.NOTPARALLEL:
|
||||
|
||||
.PHONY: fmtcheck generate protobuf website website-test staticcheck website/local website/build-local
|
||||
.PHONY: fmtcheck importscheck generate protobuf website website-test staticcheck website/local website/build-local
|
||||
|
19
scripts/goimportscheck.sh
Executable file
19
scripts/goimportscheck.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Check goimports
|
||||
echo "==> Checking the code complies with goimports requirements..."
|
||||
target_files=$(git diff --name-only origin/main | grep "\.go")
|
||||
|
||||
if [[ -z ${target_files} ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
goimports_files=$(goimports -w -l -local github.com/hashicorp/terraform/ ${target_files})
|
||||
if [[ -n ${goimports_files} ]]; then
|
||||
echo 'goimports needs running on the following files:'
|
||||
echo "${goimports_files}"
|
||||
echo "You can use the command and flags \`goimports -w -l -local github.com/hashicorp/terraform/\` to reformat the code"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue
Block a user