Skip to content

Commit d5b245a

Browse files
'make fmt' handles imports too (#12837)
Signed-off-by: David L. Chandler <[email protected]>
1 parent 3095779 commit d5b245a

File tree

123 files changed

+276
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+276
-345
lines changed

.golangci.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,18 @@ issues:
172172
uniq-by-line: true
173173
formatters:
174174
enable:
175-
- goimports
175+
- gofmt
176+
- gci
177+
settings:
178+
gci:
179+
sections:
180+
- standard
181+
- default
182+
- prefix(github.com/kgateway-dev)
183+
- blank
184+
- dot
185+
- localmodule
186+
custom-order: true
176187
exclusions:
177188
generated: lax
178189
paths:

Makefile

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ $(BUG_REPORT_DIR):
7979
# Base Alpine image used for all containers. Exported for use in goreleaser.yaml.
8080
export ALPINE_BASE_IMAGE ?= alpine:3.17.6
8181

82+
GO_VERSION := $(shell cat go.mod | grep -E '^go' | awk '{print $$2}')
83+
GOTOOLCHAIN ?= go$(GO_VERSION)
84+
85+
DEPSGOBIN ?= $(OUTPUT_DIR)
86+
GOLANGCI_LINT ?= go tool golangci-lint
87+
ANALYZE_ARGS ?= --fix --verbose
88+
CUSTOM_GOLANGCI_LINT_BIN ?= $(DEPSGOBIN)/golangci-lint-custom
89+
CUSTOM_GOLANGCI_LINT_RUN ?= $(CUSTOM_GOLANGCI_LINT_BIN) run --build-tags e2e
90+
CUSTOM_GOLANGCI_LINT_FMT ?= $(CUSTOM_GOLANGCI_LINT_BIN) fmt
91+
8292
#----------------------------------------------------------------------------------
8393
# Macros
8494
#----------------------------------------------------------------------------------
@@ -91,19 +101,17 @@ get_sources = $(shell find $(1) -name "*.go" | grep -v test | grep -v generated.
91101
# Repo setup
92102
#----------------------------------------------------------------------------------
93103

94-
GOIMPORTS ?= go tool goimports
95-
96104
.PHONY: init-git-hooks
97105
init-git-hooks: ## Use the tracked version of Git hooks from this repo
98106
git config core.hooksPath .githooks
99107

100108
.PHONY: fmt
101-
fmt: ## Format the code with goimports
102-
$(GOIMPORTS) -local "github.com/kgateway-dev/kgateway/v2/" -w $(shell ls -d */ | grep -v vendor)
109+
fmt: $(CUSTOM_GOLANGCI_LINT_BIN) ## Format the code with golangci-lint
110+
$(CUSTOM_GOLANGCI_LINT_FMT) ./...
103111

104112
.PHONY: fmt-changed
105-
fmt-changed: ## Format the code with goimports
106-
git diff --name-only | grep '.*.go$$' | xargs -- $(GOIMPORTS) -w
113+
fmt-changed: $(CUSTOM_GOLANGCI_LINT_BIN) ## Format only the changed code with golangci-lint
114+
git status -s -uno | awk '{print $2}' | grep '.*.go$$' | xargs -r $(CUSTOM_GOLANGCI_LINT_FMT)
107115

108116
# must be a separate target so that make waits for it to complete before moving on
109117
.PHONY: mod-download
@@ -123,20 +131,11 @@ mod-tidy: mod-download mod-tidy-nested ## Tidy the go mod file
123131
# Analyze
124132
#----------------------------------------------------------------------------
125133

126-
GO_VERSION := $(shell cat go.mod | grep -E '^go' | awk '{print $$2}')
127-
GOTOOLCHAIN ?= go$(GO_VERSION)
128-
129-
DEPSGOBIN ?= $(OUTPUT_DIR)
130-
GOLANGCI_LINT ?= go tool golangci-lint
131-
ANALYZE_ARGS ?= --fix --verbose
132-
133-
CUSTOM_GOLANGCI_LINT_BIN ?= $(DEPSGOBIN)/golangci-lint-custom
134134
.PHONY: analyze
135135
analyze: $(CUSTOM_GOLANGCI_LINT_BIN) ## Run golangci-lint. Override options with ANALYZE_ARGS.
136-
$(CUSTOM_GOLANGCI_LINT_BIN) run $(ANALYZE_ARGS) --build-tags e2e ./...
136+
$(CUSTOM_GOLANGCI_LINT_RUN) $(ANALYZE_ARGS) ./...
137137

138-
.PHONY: $(CUSTOM_GOLANGCI_LINT_BIN)
139-
$(CUSTOM_GOLANGCI_LINT_BIN):
138+
$(CUSTOM_GOLANGCI_LINT_BIN): go.mod go.sum .custom-gcl.yml
140139
GOTOOLCHAIN=$(GOTOOLCHAIN) $(GOLANGCI_LINT) custom
141140

142141
ACTION_LINT ?= go tool github.com/rhysd/actionlint/cmd/actionlint
@@ -340,12 +339,14 @@ clean-stamps:
340339
$(STAMP_DIR)/go-generate-apis: $(API_SOURCE_FILES) | $(STAMP_DIR)
341340
@echo "Running API code generation..."
342341
GO111MODULE=on go generate ./hack/...
342+
$(MAKE) fmt-changed
343343
@touch $@
344344

345345
# Mock generation with dependency tracking
346346
$(STAMP_DIR)/go-generate-mocks: $(MOCK_SOURCE_FILES) | $(STAMP_DIR)
347347
@echo "Running mock generation..."
348348
GO111MODULE=on go generate -run="mockgen" ./...
349+
$(MAKE) fmt-changed
349350
@touch $@
350351

351352
# Combine both generation steps
@@ -369,9 +370,9 @@ $(STAMP_DIR)/generate-licenses: $(MOD_FILES) | $(STAMP_DIR)
369370
@touch $@
370371

371372
# Formatting - only runs if generation steps changed
372-
$(STAMP_DIR)/fmt: $(STAMP_DIR)/go-generate-all
373+
$(STAMP_DIR)/fmt: $(STAMP_DIR)/go-generate-all $(CUSTOM_GOLANGCI_LINT_BIN)
373374
@echo "Formatting code..."
374-
$(GOIMPORTS) -local "github.com/kgateway-dev/kgateway/v2/" -w $(shell ls -d */ | grep -v vendor)
375+
$(CUSTOM_GOLANGCI_LINT_FMT) ./...
375376
@touch $@
376377

377378
# Fast generation using stamp files (for local development)

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

devel/contributing/code-generation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Here is a description of each sub-target and its purpose:
1313
| `go-generate-mocks` | Invokes all mockgen generate directives in the repo | There is an interface API change | 2:30-3m |
1414
| `mod-download` | Calls `go mod download all` | Dependencies are not all present locally | 1s (if deps are already downloaded) |
1515
| `mod-tidy` | Calls `go mod tidy` | Dependencies have been added, updated, or removed | 1-2s |
16-
| `fmt` | Runs [`goimports`](https://pkg.go.dev/golang.org/x/tools/cmd/goimports) which updates imports and formats code | Code has been modified (any change, in case it's not properly formatted) | 9-13s |
16+
| `fmt` | Runs golangci-lint with the `gci` formatter which organizes imports and formats code | Code has been modified (any change, in case it's not properly formatted) | 9-13s |
1717
| `clean-gen` | Removes files generated by codegen | An existing generated file may no longer be generated by codegen (rare) | 3s |
1818
| `generate-licenses` | Generates docs files containing attribution for all dependencies which require it | There is a new dependency or a depency bump | 7-12s |

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ require (
130130
go.yaml.in/yaml/v2 v2.4.3 // indirect
131131
go.yaml.in/yaml/v3 v3.0.4 // indirect
132132
go.yaml.in/yaml/v4 v4.0.0-rc.2 // indirect
133-
golang.org/x/telemetry v0.0.0-20251008203120-078029d740a8 // indirect
134133
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
135134
gotest.tools/gotestsum v1.13.0 // indirect
136135
oras.land/oras-go/v2 v2.6.0 // indirect
@@ -647,7 +646,6 @@ tool (
647646
github.com/goreleaser/goreleaser/v2
648647
github.com/onsi/ginkgo/v2/ginkgo
649648
github.com/rhysd/actionlint/cmd/actionlint
650-
golang.org/x/tools/cmd/goimports
651649
gotest.tools/gotestsum
652650
helm.sh/helm/v3/cmd/helm
653651
k8s.io/code-generator/cmd/applyconfiguration-gen

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,8 +2018,6 @@ golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
20182018
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
20192019
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
20202020
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
2021-
golang.org/x/telemetry v0.0.0-20251008203120-078029d740a8 h1:LvzTn0GQhWuvKH/kVRS3R3bVAsdQWI7hvfLHGgh9+lU=
2022-
golang.org/x/telemetry v0.0.0-20251008203120-078029d740a8/go.mod h1:Pi4ztBfryZoJEkyFTI5/Ocsu2jXyDr6iSdgJiYE/uwE=
20232021
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
20242022
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
20252023
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=

hack/generate.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,3 @@ go tool client-gen \
6868

6969
go generate ${ROOT_DIR}/internal/...
7070
go generate ${ROOT_DIR}/pkg/...
71-
72-
# fix imports of gen code
73-
go tool goimports -local "github.com/kgateway-dev/kgateway/v2/" -w ${ROOT_DIR}/${CLIENT_GEN_DIR}
74-
go tool goimports -local "github.com/kgateway-dev/kgateway/v2/" -w ${ROOT_DIR}/api

hack/utils/applier/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"github.com/kgateway-dev/kgateway/hack/utils/applier/cmd"
5+
56
_ "k8s.io/client-go/plugin/pkg/client/auth"
67
)
78

internal/envoyinit/pkg/downward/transform_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
envoyclusterv3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
66
envoycorev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
77
envoyendpointv3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
8+
"google.golang.org/protobuf/types/known/structpb"
9+
810
. "github.com/onsi/ginkgo/v2"
911
. "github.com/onsi/gomega"
10-
"google.golang.org/protobuf/types/known/structpb"
1112

1213
. "github.com/kgateway-dev/kgateway/v2/internal/envoyinit/pkg/downward"
1314
)

internal/kgateway/admin/response_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package admin_test
33
import (
44
"errors"
55

6-
. "github.com/onsi/ginkgo/v2"
7-
. "github.com/onsi/gomega"
86
corev1 "k8s.io/api/core/v1"
97
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
108

9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
1112
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/admin"
1213
)
1314

0 commit comments

Comments
 (0)