Skip to content

Commit c21d08f

Browse files
authored
Merge pull request #70 from embik/missing-lints-modules
🌱 Dynamically run code checks against modules in this repository
2 parents 71970eb + 6d9e9c4 commit c21d08f

File tree

4 files changed

+80
-63
lines changed

4 files changed

+80
-63
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,40 @@ permissions:
1414
checks: write
1515

1616
jobs:
17+
list-modules:
18+
name: list-modules
19+
20+
runs-on: ubuntu-latest
21+
22+
outputs:
23+
modules: ${{ steps.modules.outputs.modules }}
24+
25+
steps:
26+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # tag=v5.0.0
27+
- name: Calculate go version
28+
id: vars
29+
run: echo "go_version=$(make go-version)" >> $GITHUB_OUTPUT
30+
- name: Set up Go
31+
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # tag=v5.5.0
32+
with:
33+
go-version: ${{ steps.vars.outputs.go_version }}
34+
- name: make list-modules
35+
id: modules
36+
run: echo "modules=$(make list-modules)" >> $GITHUB_OUTPUT
37+
1738
golangci-lint-matrix:
39+
name: golangci-lint [${{ matrix.working-directory }}]
40+
1841
runs-on: ubuntu-latest
42+
43+
needs:
44+
- list-modules
45+
1946
strategy:
2047
fail-fast: false
2148
matrix:
22-
working-directory:
23-
- ""
24-
- examples/kind
25-
- providers/kind
26-
- examples/cluster-api
27-
- providers/cluster-api
28-
- examples/cluster-inventory-api
29-
- providers/cluster-inventory-api
30-
- examples/file
31-
- providers/file
32-
name: golangci-lint [${{ matrix.working-directory }}]
49+
working-directory: ${{ fromJSON(needs.list-modules.outputs.modules) }}
50+
3351
steps:
3452
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # tag=v5.0.0
3553
- name: Calculate go version

Makefile

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
SHELL:=/usr/bin/env bash
2525
.DEFAULT_GOAL:=help
26+
ROOT_DIR=$(abspath .)
2627

2728
#
2829
# Go.
@@ -52,12 +53,6 @@ TOOLS_BIN_DIR := $(abspath $(TOOLS_DIR)/bin)
5253
GOLANGCI_LINT := $(abspath $(TOOLS_BIN_DIR)/golangci-lint)
5354
GO_APIDIFF := $(TOOLS_BIN_DIR)/go-apidiff
5455
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
55-
EXAMPLES_KIND_DIR := $(abspath examples/kind)
56-
PROVIDERS_KIND_DIR := $(abspath providers/kind)
57-
EXAMPLES_CLUSTER_API_DIR := $(abspath examples/cluster-api)
58-
PROVIDERS_CLUSTER_API_DIR := $(abspath providers/cluster-api)
59-
EXAMPLES_CLUSTER_INVENTORY_API_DIR := $(abspath examples/cluster-inventory-api)
60-
PROVIDERS_CLUSTER_INVENTORY_API_DIR := $(abspath providers/cluster-inventory-api)
6156
GO_INSTALL := ./hack/go-install.sh
6257

6358
# The help will print out all targets with their descriptions organized bellow their categories. The categories are represented by `##@` and the target descriptions by `##`.
@@ -111,39 +106,53 @@ GO_MOD_CHECK_DIR := $(abspath ./hack/tools/cmd/gomodcheck)
111106
GO_MOD_CHECK := $(abspath $(TOOLS_BIN_DIR)/gomodcheck)
112107
GO_MOD_CHECK_IGNORE := $(abspath .gomodcheck.yaml)
113108
.PHONY: $(GO_MOD_CHECK)
114-
$(GO_MOD_CHECK): # Build gomodcheck
109+
$(GO_MOD_CHECK): # Build gomodcheck.
115110
go build -C $(GO_MOD_CHECK_DIR) -o $(GO_MOD_CHECK)
116111

117112
## --------------------------------------
118113
## Linting
119114
## --------------------------------------
120115

121116
.PHONY: lint
122-
lint: $(GOLANGCI_LINT) ## Lint codebase
123-
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
124-
cd examples/kind; $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
125-
cd proviers/kind; $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
126-
cd examples/cluster-api; $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
127-
cd proviers/cluster-api; $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
117+
lint: WHAT ?=
118+
lint: $(GOLANGCI_LINT) ## Lint codebase.
119+
@if [ -n "$(WHAT)" ]; then \
120+
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS) $(WHAT); \
121+
else \
122+
for MOD in . $$(git ls-files '**/go.mod' | sed 's,/go.mod,,'); do \
123+
(cd $$MOD; $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)); \
124+
done; \
125+
fi
128126

129127
.PHONY: lint-fix
130128
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter.
131129
GOLANGCI_LINT_EXTRA_ARGS=--fix $(MAKE) lint
132130

131+
.PHONY: imports
132+
imports: WHAT ?=
133+
imports: $(GOLANGCI_LINT) ## Format module imports.
134+
@if [ -n "$(WHAT)" ]; then \
135+
$(GOLANGCI_LINT) fmt --enable gci -c $(ROOT_DIR)/.golangci.yml $(WHAT); \
136+
else \
137+
for MOD in . $$(git ls-files '**/go.mod' | sed 's,/go.mod,,'); do \
138+
(cd $$MOD; $(GOLANGCI_LINT) fmt --enable gci -c $(ROOT_DIR)/.golangci.yml); \
139+
done; \
140+
fi
141+
133142
## --------------------------------------
134143
## Generate
135144
## --------------------------------------
136145

137146
.PHONY: modules
147+
modules: WHAT ?=
138148
modules: ## Runs go mod to ensure modules are up to date.
139-
go mod tidy
140-
cd $(TOOLS_DIR); go mod tidy
141-
cd $(EXAMPLES_KIND_DIR); go mod tidy
142-
cd $(PROVIDERS_KIND_DIR); go mod tidy
143-
cd $(EXAMPLES_CLUSTER_API_DIR); go mod tidy
144-
cd $(PROVIDERS_CLUSTER_API_DIR); go mod tidy
145-
cd $(EXAMPLES_CLUSTER_INVENTORY_API_DIR); go mod tidy
146-
cd $(PROVIDERS_CLUSTER_INVENTORY_API_DIR); go mod tidy
149+
@if [ -n "$(WHAT)" ]; then \
150+
(cd $(WHAT); go mod tidy); \
151+
else \
152+
for MOD in . $$(git ls-files '**/go.mod' | sed 's,/go.mod,,'); do \
153+
(cd $$MOD; go mod tidy); \
154+
done; \
155+
fi
147156

148157
## --------------------------------------
149158
## Cleanup / Verification
@@ -159,54 +168,42 @@ clean-bin: ## Remove all generated binaries.
159168
rm -rf hack/tools/bin
160169

161170
.PHONY: clean-release
162-
clean-release: ## Remove the release folder
171+
clean-release: ## Remove the release folder.
163172
rm -rf $(RELEASE_DIR)
164173

165174
.PHONY: verify-modules
166-
verify-modules: modules $(GO_MOD_CHECK) ## Verify go modules are up to date
167-
@if !(git diff --quiet HEAD -- go.sum go.mod $(TOOLS_DIR)/go.mod $(TOOLS_DIR)/go.sum \
168-
$(EXAMPLES_KIND_DIR)/go.mod $(EXAMPLES_KIND_DIR)/go.sum \
169-
$(PROVIDERS_KIND_DIR)/go.mod $(PROVIDERS_KIND_DIR)/go.sum \
170-
$(EXAMPLES_CLUSTER_API_DIR)/go.mod $(EXAMPLES_CLUSTER_API_DIR)/go.sum \
171-
$(PROVIDERS_CLUSTER_API_DIR)/go.mod $(PROVIDERS_CLUSTER_API_DIR)/go.sum \
172-
$(EXAMPLES_CLUSTER_INVENTORY_API_DIR)/go.mod $(EXAMPLES_CLUSTER_INVENTORY_API_DIR)/go.sum \
173-
$(PROVIDERS_CLUSTER_INVENTORY_API_DIR)/go.mod $(PROVIDERS_CLUSTER_INVENTORY_API_DIR)/go.sum \
174-
); then \
175-
git diff; \
176-
echo "go module files are out of date, please run 'make modules'"; exit 1; \
177-
fi
175+
verify-modules: modules $(GO_MOD_CHECK) ## Verify go modules are up to date.
176+
@for MOD in . $(TOOLS_DIR) $$(git ls-files '**/go.mod' | sed 's,/go.mod,,'); do \
177+
pushd $$MOD >/dev/null; if !(git diff --quiet HEAD -- go.sum go.mod); then echo "[$$MOD] go modules are out of date, please run 'make modules'"; exit 1; fi; popd >/dev/null; \
178+
done; \
179+
178180
$(GO_MOD_CHECK) $(GO_MOD_CHECK_IGNORE)
179181

180182
APIDIFF_OLD_COMMIT ?= $(shell git rev-parse origin/main)
181183

182184
.PHONY: apidiff
183-
verify-apidiff: $(GO_APIDIFF) ## Check for API differences
185+
verify-apidiff: $(GO_APIDIFF) ## Check for API differences.
184186
$(GO_APIDIFF) $(APIDIFF_OLD_COMMIT) --print-compatible
185187

186188
## --------------------------------------
187189
## Release Tooling
188190
## --------------------------------------
189191

190192

191-
.PHONY: provider-release
192-
provider-release: ## Create a commit bumping the provider modules to the latest release tag and tag providers.
193-
@./hack/release-providers.sh
193+
.PHONY: release-commit
194+
release-commit: ## Create a commit bumping the provider modules to the latest release tag and tag providers.
195+
@./hack/release-commit.sh
194196

195197
## --------------------------------------
196198
## Helpers
197199
## --------------------------------------
198200

199201
##@ helpers:
200202

201-
go-version: ## Print the go version we use to compile our binaries and images
203+
go-version: ## Print the go version we use to compile our binaries and images.
202204
@echo $(GO_VERSION)
203205

204-
WHAT ?=
205-
imports:
206-
@if [ -n "$(WHAT)" ]; then \
207-
$(GOLANGCI_LINT) run --enable-only=gci --fix --fast $(WHAT); \
208-
else \
209-
for MOD in . $$(git ls-files '**/go.mod' | sed 's,/go.mod,,'); do \
210-
(cd $$MOD; $(GOLANGCI_LINT) run --enable-only=gci --fix --fast); \
211-
done; \
212-
fi
206+
list-modules: ## Print the Go modules in this repository for GitHub Actions matrix.
207+
@echo -n '['; \
208+
git ls-files '**/go.mod' | sed 's,/go.mod,,' | awk 'BEGIN {printf "\"%s\"", "."} NR > 0 {printf ",\"%s\"", $$0}'; \
209+
echo ']'

hack/check-everything.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ export KUBEBUILDER_ASSETS="$(${tmp_bin}/setup-envtest use --use-env -p path "${E
4444
${hack_dir}/test-all.sh
4545

4646
header_text "confirming examples compile (via go install)"
47-
pushd examples/kind; go install ${MOD_OPT} .; popd
48-
pushd examples/namespace; go install ${MOD_OPT} .; popd
49-
pushd examples/cluster-api; go install ${MOD_OPT} .; popd
50-
pushd examples/cluster-inventory-api; go install ${MOD_OPT} .; popd
47+
for EXAMPLE in $(ls examples); do
48+
pushd examples/${EXAMPLE}; go install ${MOD_OPT} .; popd
49+
done
5150

5251
echo "passed"
5352
exit 0

hack/verify.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ cd "${REPO_ROOT}"
2424
header_text "running modules"
2525
make modules
2626

27+
header_text "running imports"
28+
make imports
29+
2730
# Only run verify-modules in CI, otherwise updating
2831
# go module locally (which is a valid operation) causes `make test` to fail.
2932
if [[ -n ${CI} ]]; then

0 commit comments

Comments
 (0)