Skip to content

Commit 54a784d

Browse files
Merge pull request #69 from planetscale/lint-more
cleanup: make sure to follow linter rules
2 parents bfe4db4 + 8f059b7 commit 54a784d

12 files changed

+174
-99
lines changed

.github/workflows/ci.yml

+30-11
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,58 @@ jobs:
77
test:
88
runs-on: ubuntu-latest
99
steps:
10-
- uses: actions/checkout@v4
10+
- name: Checkout code
11+
uses: actions/checkout@v4
1112

12-
- uses: actions/setup-go@v5
13+
- name: Set up Go
14+
uses: actions/setup-go@v5
1315
with:
1416
go-version-file: './go.mod'
1517

16-
- run: go version
18+
- name: Cache Go modules
19+
uses: actions/cache@v3
20+
with:
21+
path: |
22+
~/.cache/go-build
23+
~/go/pkg/mod
24+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
25+
restore-keys: |
26+
${{ runner.os }}-go-
27+
28+
- name: Install Dependencies
29+
run: go mod download
1730

18-
- name: Go mod tidy
31+
- name: Verify go.mod and go.sum
1932
run: |
2033
go mod tidy
2134
git diff --exit-code || (echo "go.mod or go.sum is not clean! Run 'go mod tidy' and commit the changes." && exit 1)
2235
23-
- name: Go Vet
24-
run: go vet ./go/...
36+
- name: Install Tools
37+
run: |
38+
make install-tools
39+
40+
- name: Display Go version
41+
run: go version
2542

2643
- name: Build
2744
run: make build
2845

29-
- name: Check formatting
46+
- name: Check Formatting
3047
run: |
3148
make pretty
3249
git diff --exit-code || (echo "Code is not formatted correctly! Run 'make pretty' and commit the changes." && exit 1)
3350
51+
- name: Run Linting
52+
run: make lint
53+
3454
- name: Install go-junit-report
3555
run: go install github.com/jstemmer/go-junit-report@latest
3656

3757
- name: Run Tests and Convert to JUnit
38-
run: go test -v ./go/... | go-junit-report > report.xml
58+
run: go test -v ./... | go-junit-report > report.xml
3959

4060
- name: Test Summary
41-
if: true
42-
uses: test-summary/action@31493c76ec9e7aa675f1585d3ed6f1da69269a86 # v2.4
61+
uses: test-summary/[email protected]
4362
with:
4463
paths: "report.xml"
45-
show: "fail"
64+
show: "fail"

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ errors/
2626
/vt
2727

2828
# Do not ignore anything inside the src/vitess-tester directory
29-
!/go
29+
!/go
30+
31+
go/testdata/expected/

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ linters-settings:
131131
gosec:
132132
excludes:
133133
- G115
134+
- G404
134135

135136
inamedparam:
136137
# Skips check for interface methods with only a single parameter.

Makefile

+38-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
.PHONY: all build test tidy clean pretty install-tools lint install-hooks
2+
.DEFAULT_GOAL := test_and_build
23

3-
GO := go
44
REQUIRED_GO_VERSION := 1.23
5-
GOLANGCI_LINT_VERSION := v1.55.2
5+
GOLANGCI_LINT_VERSION := v1.62.0
6+
7+
# Determine the Go binary directory
8+
GOBIN_DIR := $(or $(GOBIN), $(shell go env GOBIN))
9+
ifeq ($(GOBIN_DIR),)
10+
GOBIN_DIR := $(shell go env GOPATH)/bin
11+
endif
12+
13+
test_and_build: test build
614

715
# Version check
816
check_version:
9-
@GO_VERSION=$$($(GO) version | awk '{print $$3}' | sed 's/go//'); \
17+
@GO_VERSION=$$(go version | awk '{print $$3}' | sed 's/go//'); \
1018
MAJOR_VERSION=$$(echo $$GO_VERSION | cut -d. -f1); \
1119
MINOR_VERSION=$$(echo $$GO_VERSION | cut -d. -f2); \
1220
if [ "$$MAJOR_VERSION" -eq 1 ] && [ "$$MINOR_VERSION" -lt 23 ]; then \
@@ -19,41 +27,47 @@ check_version:
1927
default: check_version build
2028

2129
build:
22-
$(GO) build -o vt ./go/vt
30+
go build -o vt ./go/vt
2331

2432
test:
25-
$(GO) test -count=1 ./go/...
33+
go test -count=1 ./go/...
2634

2735
tidy:
28-
$(GO) mod tidy
36+
go mod tidy
2937

3038
clean:
31-
$(GO) clean -i ./...
39+
go clean -i ./...
3240
rm -f vt
3341

3442
# Pretty: formats the code using gofumpt and goimports-reviser
35-
pretty: install-tools
43+
pretty: check-tools
3644
@echo "Running formatting tools..."
37-
@gofumpt -l -w . >/dev/null 2>&1 || true
38-
@goimports-reviser -project-name $$(go list -m) -rm-unused -set-alias -format . >/dev/null 2>&1 || true
45+
@gofumpt -w . >/dev/null 2>&1
46+
@goimports-reviser -recursive -project-name $$(go list -m) -rm-unused -set-alias ./go >/dev/null 2>&1
3947

40-
# Install tools: Checks if the required tools are installed, installs if missing
48+
# Tools installation command
4149
install-tools:
42-
@command -v gofumpt >/dev/null 2>&1 || { \
43-
echo "Installing gofumpt..."; \
44-
go install mvdan.cc/gofumpt@latest >/dev/null 2>&1; \
45-
}
46-
@command -v goimports-reviser >/dev/null 2>&1 || { \
47-
echo "Installing goimports-reviser..."; \
48-
go install github.com/incu6us/goimports-reviser@latest >/dev/null 2>&1; \
49-
}
50-
@command -v golangci-lint >/dev/null 2>&1 || { \
51-
echo "Installing golangci-lint..."; \
52-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin $(GOLANGCI_LINT_VERSION); \
53-
}
50+
@echo "Installing gofumpt..."
51+
go install mvdan.cc/gofumpt@latest
52+
53+
@echo "Installing goimports-reviser..."
54+
go install github.com/incu6us/goimports-reviser/v3@latest
55+
56+
@echo "Installing golangci-lint..."
57+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
58+
sh -s -- -b $(GOBIN_DIR) $(GOLANGCI_LINT_VERSION)
59+
60+
@echo "All tools installed successfully."
61+
62+
# Ensure tools are available
63+
check-tools:
64+
@command -v gofumpt >/dev/null 2>&1 || { echo "gofumpt not found. Run 'make install-tools' to install it." >&2; exit 1; }
65+
@command -v goimports-reviser >/dev/null 2>&1 || { echo "goimports-reviser not found. Run 'make install-tools' to install it." >&2; exit 1; }
66+
@command -v golangci-lint >/dev/null 2>&1 || { echo "golangci_lint not found. Run 'make install-tools' to install it." >&2; exit 1; }
67+
5468

5569
# Lint: runs golangci-lint
56-
lint: install-tools
70+
lint: check-tools
5771
@echo "Running golangci-lint..."
5872
@golangci-lint run --config .golangci.yml ./go/...
5973

git-hooks/pre-commit

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ for file in $GO_FILES; do
1818
}
1919
done
2020

21-
# 2. Run goimports-reviser to verify formatting on changed files (without writing changes)
22-
echo "Checking goimports-reviser formatting..."
21+
# Check imports with goimports-reviser
22+
echo "Checking goimports-reviser imports..."
2323
for file in $GO_FILES; do
24-
goimports-reviser -project-name $(go list -m) -rm-unused -set-alias -format "$file" -diff >/dev/null 2>&1
24+
goimports-reviser -project-name $(go list -m) -rm-unused -set-alias "$file" >/dev/null 2>&1
2525
if [[ $? -ne 0 ]]; then
26-
echo "Error: Imports not correctly formatted in $file. Please run 'make pretty'."
26+
echo "Error: Imports not correctly organized in $file. Please run 'make pretty'."
2727
exit 1
2828
fi
2929
done

go/data/vtgate_log_parse.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,22 @@ func getBindVariables(bindVarsRaw string, lineNumber int) (map[string]*querypb.B
167167
var val []byte
168168
switch {
169169
case sqltypes.IsIntegral(bvType) || sqltypes.IsFloat(bvType):
170-
val = []byte(strconv.FormatFloat(value.Value.(float64), 'f', -1, 64))
170+
f, ok := value.Value.(float64)
171+
if ok {
172+
val = []byte(strconv.FormatFloat(f, 'f', -1, 64))
173+
}
171174
case bvType == sqltypes.Tuple:
172175
// the query log of vtgate does not list all the values for a tuple
173176
// instead it lists the following: "v2": {"type": "TUPLE", "value": "2 items"}
174177
return nil, fmt.Errorf("line %d: cannot parse tuple bind variables", lineNumber)
175-
default:
176-
val = []byte(value.Value.(string))
178+
}
179+
if val == nil {
180+
sval, ok := value.Value.(string)
181+
if !ok {
182+
return nil, fmt.Errorf("line %d: cannot parse bind variable value", lineNumber)
183+
}
184+
185+
val = []byte(sval)
177186
}
178187
bvProcessed[key] = &querypb.BindVariable{
179188
Type: bvType,

go/summarize/reading.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ func getDecoderAndDelim(file *os.File) (*json.Decoder, json.Delim) {
5858
if err != nil {
5959
exit("Error reading json: " + err.Error())
6060
}
61+
delim, ok := val.(json.Delim)
62+
if !ok {
63+
exit("Error reading json: expected delimiter")
64+
}
6165

6266
// Reset the file pointer to the beginning
6367
_, err = file.Seek(0, io.SeekStart)
6468
if err != nil {
6569
exit("Error rewinding file: " + err.Error())
6670
}
6771
decoder = json.NewDecoder(file)
68-
return decoder, val.(json.Delim)
72+
return decoder, delim
6973
}
7074

7175
func readTracedQueryFile(decoder *json.Decoder, fileName string) readingSummary {

go/summarize/summarize-keys.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func printKeysSummary(out io.Writer, file readingSummary, now time.Time, hotMetr
177177
summary := summarizeKeysQueries(file.AnalysedQueries, metricReader, schemaInfo)
178178

179179
renderHotQueries(md, summary.hotQueries, metricReader)
180-
renderTableUsage(summary.tables, md)
180+
renderTableUsage(summary.tables, md, schemaInfo != nil)
181181
renderTablesJoined(md, file.AnalysedQueries)
182182
renderFailures(md, summary.failures)
183183

@@ -268,7 +268,7 @@ func renderHotQueries(md *markdown.MarkDown, queries []keys.QueryAnalysisResult,
268268
}
269269
}
270270

271-
func renderTableUsage(tableSummaries []TableSummary, md *markdown.MarkDown) {
271+
func renderTableUsage(tableSummaries []TableSummary, md *markdown.MarkDown, includeRowCount bool) {
272272
if len(tableSummaries) == 0 {
273273
return
274274
}
@@ -278,24 +278,31 @@ func renderTableUsage(tableSummaries []TableSummary, md *markdown.MarkDown) {
278278
})
279279

280280
md.PrintHeader("Tables", 2)
281-
renderTableOverview(md, tableSummaries)
281+
renderTableOverview(md, tableSummaries, includeRowCount)
282282

283283
md.PrintHeader("Column Usage", 3)
284284
for _, summary := range tableSummaries {
285285
renderColumnUsageTable(md, summary)
286286
}
287287
}
288288

289-
func renderTableOverview(md *markdown.MarkDown, tableSummaries []TableSummary) {
290-
headers := []string{"Table Name", "Reads", "Writes", "Number of Rows"}
289+
func renderTableOverview(md *markdown.MarkDown, tableSummaries []TableSummary, includeRowCount bool) {
290+
headers := []string{"Table Name", "Reads", "Writes"}
291+
if includeRowCount {
292+
headers = append(headers, "Number of Rows")
293+
}
291294
var rows [][]string
292295
for _, summary := range tableSummaries {
293-
rows = append(rows, []string{
296+
thisRow := []string{
294297
summary.Table,
295298
strconv.Itoa(summary.ReadQueryCount),
296299
strconv.Itoa(summary.WriteQueryCount),
297-
strconv.Itoa(summary.RowCount),
298-
})
300+
}
301+
if includeRowCount {
302+
thisRow = append(thisRow, strconv.Itoa(summary.RowCount))
303+
}
304+
305+
rows = append(rows, thisRow)
299306
}
300307
md.PrintTable(headers, rows)
301308
}

go/summarize/summarize-keys_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestSummarizeKeysWithHotnessFile(t *testing.T) {
9696
require.NoError(t, err)
9797
sb := &strings.Builder{}
9898
now := time.Date(2024, time.January, 1, 1, 2, 3, 0, time.UTC)
99-
printKeysSummary(sb, file, now, metric, "../testdata/bigger_slow_query_schema_info.json")
99+
printKeysSummary(sb, file, now, metric, "")
100100
expected, err := os.ReadFile(fmt.Sprintf("../testdata/bigger_slow_log_%s.md", metric))
101101
require.NoError(t, err)
102102
assert.Equal(t, string(expected), sb.String())

go/summarize/utils.go

+29-26
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const (
3232
dbInfoFile
3333
)
3434

35-
var fileTypeMap = map[string]fileType{
35+
var fileTypeMap = map[string]fileType{ //nolint:gochecknoglobals // this is instead of a const
3636
"trace": traceFile,
3737
"keys": keysFile,
3838
"dbinfo": dbInfoFile,
@@ -43,47 +43,50 @@ var fileTypeMap = map[string]fileType{
4343
func getFileType(filename string) (fileType, error) {
4444
file, err := os.Open(filename)
4545
if err != nil {
46-
return unknownFile, errors.New(fmt.Sprintf("error opening file: %v", err))
46+
return unknownFile, fmt.Errorf("error opening file: %v", err)
4747
}
4848
defer file.Close()
4949

5050
decoder := json.NewDecoder(file)
5151

5252
token, err := decoder.Token()
5353
if err != nil {
54-
return unknownFile, errors.New(fmt.Sprintf("Error reading token: %v", err))
54+
return unknownFile, fmt.Errorf("error reading token: %v", err)
5555
}
5656

5757
if delim, ok := token.(json.Delim); !ok || delim != '{' {
58-
return unknownFile, errors.New(fmt.Sprintf("Expected start of object '{'"))
58+
return unknownFile, errors.New("expected start of object '{'")
5959
}
6060

61-
for decoder.More() {
62-
keyToken, err := decoder.Token()
63-
if err != nil {
64-
return unknownFile, errors.New(fmt.Sprintf("Error reading key token: %v", err))
65-
}
61+
if !decoder.More() {
62+
return unknownFile, nil
63+
}
6664

67-
key, ok := keyToken.(string)
68-
if !ok {
69-
return unknownFile, errors.New(fmt.Sprintf("Expected key to be a string: %s", keyToken))
70-
}
65+
keyToken, err := decoder.Token()
66+
if err != nil {
67+
return unknownFile, fmt.Errorf("error reading key token: %v", err)
68+
}
7169

72-
valueToken, err := decoder.Token()
73-
if err != nil {
74-
return unknownFile, errors.New(fmt.Sprintf("Error reading value token: %v", err))
75-
}
70+
key, ok := keyToken.(string)
71+
if !ok {
72+
return unknownFile, fmt.Errorf("expected key to be a string: %s", keyToken)
73+
}
74+
75+
valueToken, err := decoder.Token()
76+
if err != nil {
77+
return unknownFile, fmt.Errorf("error reading value token: %v", err)
78+
}
7679

77-
if key == "fileType" {
78-
if fileType, ok := fileTypeMap[valueToken.(string)]; ok {
79-
return fileType, nil
80-
} else {
81-
return unknownFile, errors.New(fmt.Sprintf("Unknown FileType: %s", valueToken))
82-
}
83-
} else {
84-
// Currently we expect the first key to be FileType, for optimization reasons
85-
return unknownFile, nil
80+
if key == "fileType" {
81+
s, ok := valueToken.(string)
82+
if !ok {
83+
return unknownFile, fmt.Errorf("expected value to be a string: %s", valueToken)
84+
}
85+
if fileType, ok := fileTypeMap[s]; ok {
86+
return fileType, nil
8687
}
88+
return unknownFile, fmt.Errorf("unknown FileType: %s", valueToken)
8789
}
90+
8891
return unknownFile, nil
8992
}

0 commit comments

Comments
 (0)