Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Setup go-imports-organizer and organize Go imports #357

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,21 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: fmt
fmt: goimports ## Format the code using goimports
find . -not -path '*/\.*' -name '*.go' -exec $(GOIMPORTS) -w {} \;
fmt: ## Run go fmt verifications
hack/verify-gofmt.sh
Copy link
Member

@rm3l rm3l May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used to run make fmt to have the code formatted, but with this, it would just verify and exit, which is kinda surprising to me. If we are just calling ./hack/verify-gofmt.sh, I would suggest renaming this Make target into verify-fmt to make the intent clearer..

I noticed that goio also formats the code as well, besides organizing the imports, right? So maybe this target could just be this instead:

fmt: imports ## Format the code using goio

Then a separate verify-fmt target that would run hack/verify-gofmt.sh.

WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need fmt verification at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree fmt verification might not be necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coreydaley WDYT?


fmt_license: addlicense ## Ensure the license header is set on all files
$(ADDLICENSE) -v -f license_header.txt $$(find . -not -path '*/\.*' -name '*.go')
$(ADDLICENSE) -v -f license_header.txt $$(find . -name '*ockerfile')

.PHONY: imports
imports: addgoio ## Organize imports in go files using goio.
$(GOIO)

.PHONY: verify-imports
verify-imports: addgoio ## Run import verifications.
$(GOIO) -l

.PHONY: gosec
gosec: addgosec ## run the gosec scanner for non-test files in this repo
# we let the report content trigger a failure using the GitHub Security features.
Expand All @@ -127,24 +135,24 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests. We need LOCALBIN=$(LOCALBIN) to get correct default-config path
test: manifests generate verify-imports fmt vet envtest ## Run tests. We need LOCALBIN=$(LOCALBIN) to get correct default-config path
mkdir -p $(LOCALBIN)/default-config && cp config/manager/$(CONF_DIR)/* $(LOCALBIN)/default-config
LOCALBIN=$(LOCALBIN) KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(PKGS) -coverprofile cover.out

.PHONY: integration-test
integration-test: ginkgo manifests generate fmt vet envtest ## Run integration_tests. We need LOCALBIN=$(LOCALBIN) to get correct default-config path
integration-test: ginkgo manifests generate verify-imports fmt vet envtest ## Run integration_tests. We need LOCALBIN=$(LOCALBIN) to get correct default-config path
mkdir -p $(LOCALBIN)/default-config && cp config/manager/$(CONF_DIR)/* $(LOCALBIN)/default-config
LOCALBIN=$(LOCALBIN) KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) -v -r $(ARGS) integration_tests


##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
build: generate verify-imports fmt vet ## Build manager binary.
go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet build ## Run a controller from your host.
run: manifests generate verify-imports fmt vet build ## Run a controller from your host.
cd $(LOCALBIN) && mkdir -p default-config && cp ../config/manager/$(CONF_DIR)/* default-config && ./manager

# by default images expire from quay registry after 14 days
Expand Down Expand Up @@ -223,20 +231,20 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
GOIMPORTS ?= $(LOCALBIN)/goimports
ADDLICENSE ?= $(LOCALBIN)/addlicense
GOSEC ?= $(LOCALBIN)/gosec
GOIO ?= $(LOCALBIN)/goio

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.11.3
GOLANGCI_LINT_VERSION ?= v1.55.2
GOIMPORTS_VERSION ?= v0.15.0
ADDLICENSE_VERSION ?= v1.1.1
# opm and operator-sdk version
OPM_VERSION ?= v1.36.0
OPERATOR_SDK_VERSION ?= v1.33.0
GOSEC_VERSION ?= v2.18.2
GOIO_VERSION ?= v1.4.0

## Gosec options - default format is sarif so we can integrate with Github code scanning
GOSEC_FMT ?= sarif # for other options, see https://github.com/securego/gosec#output-formats
Expand Down Expand Up @@ -266,10 +274,10 @@ golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

.PHONY: goimports
goimports: $(GOIMPORTS) ## Download goimports locally if necessary.
$(GOIMPORTS): $(LOCALBIN)
test -s $(LOCALBIN)/goimports || GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@$(GOIMPORTS_VERSION)
.PHONY: addgoio
addgoio: $(GOIO) ## Download goio locally if necessary.
$(GOIO): $(LOCALBIN)
test -s $(LOCALBIN)/goio || GOBIN=$(LOCALBIN) go install github.com/go-imports-organizer/goio@$(GOIO_VERSION)

.PHONY: addlicense
addlicense: $(ADDLICENSE) ## Download addlicense locally if necessary.
Expand Down Expand Up @@ -322,7 +330,7 @@ bundle: operator-sdk manifests kustomize ## Generate bundle manifests and metada

## to update the CSV with a new tagged version of the operator:
## yq '.spec.install.spec.deployments[0].spec.template.spec.containers[1].image|="quay.io/rhdh/operator:some-other-tag"' bundle/manifests/backstage-operator.clusterserviceversion.yaml
## or
## or
## sed -r -e "s#(image: +)quay.io/.+operator.+#\1quay.io/rhdh/operator:some-other-tag#g" -i bundle/manifests/backstage-operator.clusterserviceversion.yaml
.PHONY: bundle-build
bundle-build: ## Build the bundle image.
Expand Down Expand Up @@ -413,5 +421,3 @@ show-img:

show-container-engine:
@echo -n $(CONTAINER_ENGINE)


32 changes: 12 additions & 20 deletions controllers/backstage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,27 @@ import (
"fmt"
"reflect"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"k8s.io/apimachinery/pkg/runtime/schema"

"k8s.io/apimachinery/pkg/types"

openshift "github.com/openshift/api/route/v1"

"k8s.io/apimachinery/pkg/api/meta"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

corev1 "k8s.io/api/core/v1"

appsv1 "k8s.io/api/apps/v1"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

bs "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
)

var watchedConfigSelector = metav1.LabelSelector{
Expand Down
8 changes: 5 additions & 3 deletions controllers/preprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ package controller
import (
"context"
"os"
"redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"github.com/stretchr/testify/assert"

"redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
)

func updateConfigMap(t *testing.T) BackstageReconciler {
Expand Down
10 changes: 3 additions & 7 deletions controllers/spec_preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ import (
"os"
"strconv"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"

"sigs.k8s.io/controller-runtime/pkg/log"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

bs "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
)

const AutoSyncEnvVar = "EXT_CONF_SYNC_backstage"
Expand Down
26 changes: 26 additions & 0 deletions goio.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
excludes:
- matchtype: name
regexp: ^\.git$
- matchtype: name
regexp: ^vendor$
groups:
- description: standard
matchorder: 3
regexp:
- ^[a-zA-Z0-9\/]+$
- description: kubernetes
matchorder: 1
regexp:
- ^k8s\.io|^sigs\.k8s\.io
- description: openshift
matchorder: 2
regexp:
- ^github\.com\/openshift
- description: other
matchorder: 4
regexp:
- '[a-zA-Z0-9]+\.[a-zA-Z0-9]+/'
- description: module
matchorder: 0
regexp:
- "%{module}%"
20 changes: 20 additions & 0 deletions hack/verify-gofmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

function list_go_src_files() {
find . -not \( \
\( \
-wholename './_output' \
-o -wholename './.*' \
-o -wholename '*/vendor/*' \
\) -prune \
\) -name '*.go' | sort -u
}


bad_files=$(list_go_src_files | xargs gofmt -s -l)
if [[ -n "${bad_files}" ]]; then
echo "!!! gofmt needs to be run on the listed files"
echo "${bad_files}"
echo "Try running 'gofmt -s -w [path]'"
exit 1
fi
16 changes: 6 additions & 10 deletions integration_tests/config-refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@ package integration_tests
import (
"context"
"fmt"
"redhat-developer/red-hat-developer-hub-operator/pkg/utils"
"strings"
"time"

"sigs.k8s.io/controller-runtime/pkg/client"

corev1 "k8s.io/api/core/v1"

appsv1 "k8s.io/api/apps/v1"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
"redhat-developer/red-hat-developer-hub-operator/pkg/utils"
)

var _ = When("create backstage with external configuration", func() {
Expand Down
18 changes: 6 additions & 12 deletions integration_tests/cr-config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,18 @@ import (
"context"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

"redhat-developer/red-hat-developer-hub-operator/pkg/utils"

appsv1 "k8s.io/api/apps/v1"

"sigs.k8s.io/controller-runtime/pkg/reconcile"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

"k8s.io/apimachinery/pkg/types"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
"redhat-developer/red-hat-developer-hub-operator/pkg/utils"
)

var _ = When("create backstage with CR configured", func() {
Expand Down
17 changes: 6 additions & 11 deletions integration_tests/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@ import (
"fmt"
"time"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

corev1 "k8s.io/api/core/v1"

appsv1 "k8s.io/api/apps/v1"

"sigs.k8s.io/controller-runtime/pkg/reconcile"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

"k8s.io/apimachinery/pkg/types"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
)

var _ = When("create backstage with CR configured", func() {
Expand Down
14 changes: 5 additions & 9 deletions integration_tests/default-config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ import (
"fmt"
"time"

"redhat-developer/red-hat-developer-hub-operator/pkg/utils"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

corev1 "k8s.io/api/core/v1"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
"redhat-developer/red-hat-developer-hub-operator/pkg/utils"
)

var _ = When("create default backstage", func() {
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package integration_tests
import (
"fmt"

corev1 "k8s.io/api/core/v1"

"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
corev1 "k8s.io/api/core/v1"
)

// Matcher for Container VolumeMounts
Expand Down
11 changes: 4 additions & 7 deletions integration_tests/rhdh-config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ import (
"context"
"time"

"redhat-developer/red-hat-developer-hub-operator/pkg/utils"

appsv1 "k8s.io/api/apps/v1"

"redhat-developer/red-hat-developer-hub-operator/pkg/model"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

bsv1alpha1 "redhat-developer/red-hat-developer-hub-operator/api/v1alpha1"
"redhat-developer/red-hat-developer-hub-operator/pkg/model"
"redhat-developer/red-hat-developer-hub-operator/pkg/utils"
)

var _ = When("create default backstage", func() {
Expand Down
Loading