Skip to content

Commit b2f9f6e

Browse files
committed
Add Make Target for organizing go imports
Signed-off-by: Anish Asthana <[email protected]>
1 parent c9929bd commit b2f9f6e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ LISTER_GEN ?= $(LOCALBIN)/lister-gen
278278
INFORMER_GEN ?= $(LOCALBIN)/informer-gen
279279
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
280280
ENVTEST ?= $(LOCALBIN)/setup-envtest
281+
OPENSHIFT-GOIMPORTS ?= $(LOCALBIN)/openshift-goimports
281282
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
282283
GH_CLI ?= $(LOCALBIN)/gh
283284

@@ -338,6 +339,11 @@ envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
338339
$(ENVTEST): $(LOCALBIN)
339340
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
340341

342+
.PHONY: openshift-goimports
343+
openshift-goimports: $(OPENSHIFT-GOIMPORTS) ## Download openshift-goimports locally if necessary.
344+
$(OPENSHIFT-GOIMPORTS): $(LOCALBIN)
345+
test -s $(LOCALBIN)/openshift-goimports || GOBIN=$(LOCALBIN) go install github.com/openshift-eng/openshift-goimports@latest
346+
341347
OPERATOR_SDK_DL_URL := https://github.com/operator-framework/operator-sdk/releases/download/$(OPERATOR_SDK_VERSION)
342348
.PHONY: install-operator-sdk
343349
install-operator-sdk: $(OPERATOR_SDK) ## Download fixed version operator-sdk binary for consist outcome
@@ -440,3 +446,11 @@ test-e2e: defaults manifests generate fmt vet ## Run e2e tests.
440446
.PHONY: setup-e2e
441447
setup-e2e: ## Set up e2e tests.
442448
KUBERAY_VERSION=$(KUBERAY_VERSION) test/e2e/setup.sh
449+
450+
.PHONY: imports
451+
imports: openshift-goimports ## Organize imports in go files using openshift-goimports. Example: make imports
452+
$(OPENSHIFT-GOIMPORTS)
453+
454+
.PHONY: verify-imports
455+
verify-imports: openshift-goimports ## Run import verifications.
456+
./hack/verify-imports.sh $(OPENSHIFT-GOIMPORTS)

hack/tools.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build tools
2+
// +build tools
3+
4+
package hack
5+
6+
// Add tools that hack scripts depend on here, to ensure they are vendored.
7+
import (
8+
_ "github.com/openshift-eng/openshift-goimports"
9+
)

hack/verify-imports.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
OPENSHIFT_GOIMPORTS=${1}
3+
4+
# ${OPENSHIFT_GOIMPORTS} -l
5+
bad_files=$(${OPENSHIFT_GOIMPORTS} -l)
6+
7+
echo $bad_files
8+
if [[ -n ${bad_files} ]]; then
9+
echo "!!! openshift-goimports needs to be run on the following files:"
10+
echo "${bad_files}"
11+
echo "Try running 'make imports'"
12+
exit 1
13+
fi

0 commit comments

Comments
 (0)