Skip to content

Commit 88fadee

Browse files
authored
Enable golangci-lint and fix issues (#268)
Signed-off-by: Stephen Finucane <[email protected]>
1 parent e4fc39f commit 88fadee

File tree

37 files changed

+351
-302
lines changed

37 files changed

+351
-302
lines changed

.github/workflows/unit.yml

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
on: [push, pull_request]
21
name: Unit Testing
2+
on: [push, pull_request]
33
jobs:
44
test:
55
runs-on: ubuntu-latest
@@ -9,33 +9,19 @@ jobs:
99
go-version:
1010
- "1.22"
1111

12-
env:
13-
GO111MODULE: "on"
14-
1512
steps:
13+
- name: Checkout source
14+
uses: actions/checkout@v5
15+
1616
- name: Setup Go ${{ matrix.go-version }}
1717
uses: actions/setup-go@v5
1818
with:
1919
go-version: ${{ matrix.go-version }}
2020

21-
- uses: actions/checkout@v5
22-
23-
- name: Setup environment
21+
- name: Run linters
2422
run: |
25-
# Changing into a different directory to avoid polluting go.sum with "go get"
26-
cd "$(mktemp -d)"
27-
go mod init unit_tests
28-
29-
go install golang.org/x/tools/cmd/goimports@latest
30-
31-
- name: Run go vet
32-
run: |
33-
go vet ./...
23+
make lint
3424
3525
- name: Run unit tests
3626
run: |
37-
go test -v ./...
38-
39-
- name: Check for formatting
40-
run:
41-
./script/format
27+
make unit

.golangci.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: "2"
2+
linters:
3+
default: none
4+
enable:
5+
- errcheck
6+
- govet
7+
- staticcheck
8+
- unparam
9+
- unused
10+
exclusions:
11+
generated: lax
12+
presets:
13+
- comments
14+
- common-false-positives
15+
- legacy
16+
- std-error-handling
17+
paths:
18+
- third_party$
19+
- builtin$
20+
- examples$
21+
formatters:
22+
enable:
23+
- gofmt
24+
- goimports
25+
exclusions:
26+
generated: lax
27+
paths:
28+
- third_party$
29+
- builtin$
30+
- examples$

Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
undefine GOFLAGS
2+
3+
GOLANGCI_LINT_VERSION?=v2.1.6
4+
GOTESTSUM_VERSION?=v1.12.2
5+
GO_TEST?=go run gotest.tools/gotestsum@$(GOTESTSUM_VERSION) --format testname --
6+
TIMEOUT := "60m"
7+
8+
ifeq ($(shell command -v podman 2> /dev/null),)
9+
RUNNER=docker
10+
else
11+
RUNNER=podman
12+
endif
13+
14+
# if the golangci-lint steps fails with one of the following error messages:
15+
#
16+
# directory prefix . does not contain main module or its selected dependencies
17+
#
18+
# failed to initialize build cache at /root/.cache/golangci-lint: mkdir /root/.cache/golangci-lint: permission denied
19+
#
20+
# you probably have to fix the SELinux security context for root directory plus your cache
21+
#
22+
# chcon -Rt svirt_sandbox_file_t .
23+
# chcon -Rt svirt_sandbox_file_t ~/.cache/golangci-lint
24+
lint:
25+
mkdir -p ~/.cache/golangci-lint/$(GOLANGCI_LINT_VERSION)
26+
$(RUNNER) run -t --rm \
27+
-v $(shell pwd):/app \
28+
-v ~/.cache/golangci-lint/$(GOLANGCI_LINT_VERSION):/root/.cache \
29+
-w /app \
30+
golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) golangci-lint run -v --max-same-issues 50
31+
.PHONY: lint
32+
33+
format:
34+
gofmt -w -s $(shell pwd)
35+
.PHONY: format
36+
37+
unit:
38+
$(GO_TEST) -shuffle on ./...
39+
.PHONY: unit
40+
41+
coverage:
42+
$(GO_TEST) -shuffle on -covermode count -coverprofile cover.out -coverpkg=./... ./...
43+
.PHONY: coverage

client/client.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"log"
1110
"net/http"
1211
"sort"
@@ -19,22 +18,22 @@ import (
1918

2019
// Logger is an interface representing the Logger struct
2120
type Logger interface {
22-
Printf(format string, args ...interface{})
21+
Printf(format string, args ...any)
2322
}
2423

2524
// DefaultLogger is a default struct, which satisfies the Logger interface
2625
type DefaultLogger struct{}
2726

2827
// Printf is a default Printf method
29-
func (DefaultLogger) Printf(format string, args ...interface{}) {
28+
func (DefaultLogger) Printf(format string, args ...any) {
3029
log.Printf("[DEBUG] "+format, args...)
3130
}
3231

3332
// noopLogger is a default noop logger satisfies the Logger interface
3433
type noopLogger struct{}
3534

3635
// Printf is a default noop method
37-
func (noopLogger) Printf(format string, args ...interface{}) {}
36+
func (noopLogger) Printf(format string, args ...any) {}
3837

3938
// RoundTripper satisfies the http.RoundTripper interface and is used to
4039
// customize the default http client RoundTripper
@@ -101,9 +100,7 @@ func (rt *RoundTripper) SetHeaders(headers http.Header) {
101100
newHeaders := make(http.Header, len(headers))
102101
for k, v := range headers {
103102
s := make([]string, len(v))
104-
for i, v := range v {
105-
s[i] = v
106-
}
103+
copy(s, v)
107104
newHeaders[k] = s
108105
}
109106

@@ -181,7 +178,7 @@ func (rt *RoundTripper) RoundTrip(request *http.Request) (*http.Response, error)
181178
// this is concurrency safe
182179
ort := rt.Rt
183180
if ort == nil {
184-
return nil, fmt.Errorf("Rt RoundTripper is nil, aborting")
181+
return nil, fmt.Errorf("Rt RoundTripper is nil, aborting") //nolint
185182
}
186183
response, err := ort.RoundTrip(request)
187184

@@ -232,7 +229,7 @@ func (rt *RoundTripper) logRequest(original io.ReadCloser, contentType string) (
232229
}
233230
rt.log().Printf("OpenStack Request Body: %s", debugInfo)
234231

235-
return ioutil.NopCloser(strings.NewReader(bs.String())), nil
232+
return io.NopCloser(strings.NewReader(bs.String())), nil
236233
}
237234

238235
rt.log().Printf("Not logging because OpenStack request body isn't JSON")
@@ -259,7 +256,7 @@ func (rt *RoundTripper) logResponse(original io.ReadCloser, contentType string)
259256
rt.log().Printf("OpenStack Response Body: %s", debugInfo)
260257
}
261258

262-
return ioutil.NopCloser(strings.NewReader(bs.String())), nil
259+
return io.NopCloser(strings.NewReader(bs.String())), nil
263260
}
264261

265262
rt.log().Printf("Not logging because OpenStack response body isn't JSON")
@@ -288,14 +285,14 @@ func (rt *RoundTripper) log() Logger {
288285
// FormatJSON is a default function to pretty-format a JSON body.
289286
// It will also mask known fields which contain sensitive information.
290287
func FormatJSON(raw []byte) (string, error) {
291-
var rawData interface{}
288+
var rawData any
292289

293290
err := json.Unmarshal(raw, &rawData)
294291
if err != nil {
295292
return string(raw), fmt.Errorf("unable to parse OpenStack JSON: %s", err)
296293
}
297294

298-
data, ok := rawData.(map[string]interface{})
295+
data, ok := rawData.(map[string]any)
299296
if !ok {
300297
pretty, err := json.MarshalIndent(rawData, "", " ")
301298
if err != nil {
@@ -306,32 +303,32 @@ func FormatJSON(raw []byte) (string, error) {
306303
}
307304

308305
// Mask known password fields
309-
if v, ok := data["auth"].(map[string]interface{}); ok {
306+
if v, ok := data["auth"].(map[string]any); ok {
310307
// v2 auth methods
311-
if v, ok := v["passwordCredentials"].(map[string]interface{}); ok {
308+
if v, ok := v["passwordCredentials"].(map[string]any); ok {
312309
v["password"] = "***"
313310
}
314-
if v, ok := v["token"].(map[string]interface{}); ok {
311+
if v, ok := v["token"].(map[string]any); ok {
315312
v["id"] = "***"
316313
}
317314
// v3 auth methods
318-
if v, ok := v["identity"].(map[string]interface{}); ok {
319-
if v, ok := v["password"].(map[string]interface{}); ok {
320-
if v, ok := v["user"].(map[string]interface{}); ok {
315+
if v, ok := v["identity"].(map[string]any); ok {
316+
if v, ok := v["password"].(map[string]any); ok {
317+
if v, ok := v["user"].(map[string]any); ok {
321318
v["password"] = "***"
322319
}
323320
}
324-
if v, ok := v["application_credential"].(map[string]interface{}); ok {
321+
if v, ok := v["application_credential"].(map[string]any); ok {
325322
v["secret"] = "***"
326323
}
327-
if v, ok := v["token"].(map[string]interface{}); ok {
324+
if v, ok := v["token"].(map[string]any); ok {
328325
v["id"] = "***"
329326
}
330327
}
331328
}
332329

333330
// Mask EC2 access id and body hash
334-
if v, ok := data["credentials"].(map[string]interface{}); ok {
331+
if v, ok := data["credentials"].(map[string]any); ok {
335332
var access string
336333
if s, ok := v["access"]; ok {
337334
access, _ = s.(string)
@@ -340,17 +337,17 @@ func FormatJSON(raw []byte) (string, error) {
340337
if _, ok := v["body_hash"]; ok {
341338
v["body_hash"] = "***"
342339
}
343-
if v, ok := v["headers"].(map[string]interface{}); ok {
340+
if v, ok := v["headers"].(map[string]any); ok {
344341
if _, ok := v["Authorization"]; ok {
345342
if s, ok := v["Authorization"].(string); ok {
346-
v["Authorization"] = strings.Replace(s, access, "***", -1)
343+
v["Authorization"] = strings.ReplaceAll(s, access, "***")
347344
}
348345
}
349346
}
350347
}
351348

352349
// Ignore the huge catalog output
353-
if v, ok := data["token"].(map[string]interface{}); ok {
350+
if v, ok := data["token"].(map[string]any); ok {
354351
if _, ok := v["catalog"]; ok {
355352
v["catalog"] = "***"
356353
}

client/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Example usage with the custom logger:
6666
Prefix string
6767
}
6868
69-
func (l myLogger) Printf(format string, args ...interface{}) {
70-
log.Debugf("%s [DEBUG] "+format, append([]interface{}{l.Prefix}, args...)...)
69+
func (l myLogger) Printf(format string, args ...any) {
70+
log.Debugf("%s [DEBUG] "+format, append([]any{l.Prefix}, args...)...)
7171
}
7272
7373
func NewComputeV2Client() (*gophercloud.ServiceClient, error) {

gnocchi/metric/v1/archivepolicies/requests.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Get(ctx context.Context, c *gophercloud.ServiceClient, archivePolicyName st
2323

2424
// CreateOptsBuilder allows extensions to add additional parameters to the Create request.
2525
type CreateOptsBuilder interface {
26-
ToArchivePolicyCreateMap() (map[string]interface{}, error)
26+
ToArchivePolicyCreateMap() (map[string]any, error)
2727
}
2828

2929
// CreateOpts specifies parameters of a new Archive Policy.
@@ -62,7 +62,7 @@ type ArchivePolicyDefinitionOpts struct {
6262
}
6363

6464
// ToArchivePolicyCreateMap constructs a request body from CreateOpts.
65-
func (opts CreateOpts) ToArchivePolicyCreateMap() (map[string]interface{}, error) {
65+
func (opts CreateOpts) ToArchivePolicyCreateMap() (map[string]any, error) {
6666
return gophercloud.BuildRequestBody(opts, "")
6767
}
6868

@@ -82,7 +82,7 @@ func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateO
8282

8383
// UpdateOptsBuilder allows extensions to add additional parameters to the Update request.
8484
type UpdateOptsBuilder interface {
85-
ToArchivePolicyUpdateMap() (map[string]interface{}, error)
85+
ToArchivePolicyUpdateMap() (map[string]any, error)
8686
}
8787

8888
// UpdateOpts represents options used to update an archive policy.
@@ -93,7 +93,7 @@ type UpdateOpts struct {
9393
}
9494

9595
// ToArchivePolicyUpdateMap constructs a request body from UpdateOpts.
96-
func (opts UpdateOpts) ToArchivePolicyUpdateMap() (map[string]interface{}, error) {
96+
func (opts UpdateOpts) ToArchivePolicyUpdateMap() (map[string]any, error) {
9797
return gophercloud.BuildRequestBody(opts, "")
9898
}
9999

gnocchi/metric/v1/archivepolicies/testing/requests_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestListArchivePolicies(t *testing.T) {
2323
w.Header().Add("Content-Type", "application/json")
2424
w.WriteHeader(http.StatusOK)
2525

26-
fmt.Fprintf(w, ArchivePoliciesListResult)
26+
fmt.Fprint(w, ArchivePoliciesListResult)
2727
})
2828

2929
expected := ListArchivePoliciesExpected
@@ -56,7 +56,7 @@ func TestListArchivePoliciesAllPages(t *testing.T) {
5656
w.Header().Add("Content-Type", "application/json")
5757
w.WriteHeader(http.StatusOK)
5858

59-
fmt.Fprintf(w, ArchivePoliciesListResult)
59+
fmt.Fprint(w, ArchivePoliciesListResult)
6060
})
6161

6262
allPages, err := archivepolicies.List(fake.ServiceClient()).AllPages(context.TODO())
@@ -76,7 +76,7 @@ func TestGetArchivePolicy(t *testing.T) {
7676
w.Header().Add("Content-Type", "application/json")
7777
w.WriteHeader(http.StatusOK)
7878

79-
fmt.Fprintf(w, ArchivePolicyGetResult)
79+
fmt.Fprint(w, ArchivePolicyGetResult)
8080
})
8181

8282
s, err := archivepolicies.Get(context.TODO(), fake.ServiceClient(), "test_policy").Extract()
@@ -117,7 +117,7 @@ func TestCreate(t *testing.T) {
117117
w.Header().Add("Content-Type", "application/json")
118118
w.WriteHeader(http.StatusCreated)
119119

120-
fmt.Fprintf(w, ArchivePolicyCreateResponse)
120+
fmt.Fprint(w, ArchivePolicyCreateResponse)
121121
})
122122

123123
opts := archivepolicies.CreateOpts{
@@ -177,7 +177,7 @@ func TestUpdateArchivePolicy(t *testing.T) {
177177
w.Header().Add("Content-Type", "application/json")
178178
w.WriteHeader(http.StatusOK)
179179

180-
fmt.Fprintf(w, ArchivePolicyUpdateResponse)
180+
fmt.Fprint(w, ArchivePolicyUpdateResponse)
181181
})
182182

183183
updateOpts := archivepolicies.UpdateOpts{

0 commit comments

Comments
 (0)