Skip to content

Commit 920d73d

Browse files
Update linter (elastic#3170)
* Update linter version * Fix or disable linter warnings
1 parent 4d54af4 commit 920d73d

22 files changed

+49
-34
lines changed

Diff for: .github/workflows/golangci-lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: golangci/golangci-lint-action@v2
3939
with:
4040
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
41-
version: v1.51.2
41+
version: v1.55.2
4242

4343
# Give the job more time to execute.
4444
# Regarding `--whole-files`, the linter is supposed to support linting of changed a patch only but,

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ check-headers: ## - Check copyright headers
119119

120120
.PHONY: check-go
121121
check-go: ## - Run golangci-lint
122-
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/d58dbde584c801091e74a00940e11ff18c6c68bd/install.sh | sh -s v1.51.1
122+
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/d58dbde584c801091e74a00940e11ff18c6c68bd/install.sh | sh -s v1.55.2
123123
@./bin/golangci-lint run -v
124124

125125
.PHONY: notice

Diff for: internal/pkg/action/dispatcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func NewDispatcher(am monitor.SimpleMonitor, throttle time.Duration, i int) *Dis
5757
// Run starts the Dispatcher.
5858
// After the Dispatcher is started subscriptions may receive actions.
5959
// Subscribe may be called before or after Run.
60-
func (d *Dispatcher) Run(ctx context.Context) (err error) {
60+
func (d *Dispatcher) Run(ctx context.Context) error {
6161
for {
6262
select {
6363
case <-ctx.Done():
64-
return
64+
return nil
6565
case hits := <-d.am.Output():
6666
d.process(ctx, hits)
6767
}

Diff for: internal/pkg/api/handleCheckin.go

+3
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ func filterActions(zlog zerolog.Logger, agentID string, actions []model.Action)
638638
// raw is first parsed into the action-specific data struct then passed into Action_Data in order to remove any undefined keys.
639639
//
640640
// TODO: There is a lot of repitition in this method we should try to clean up.
641+
//
642+
//nolint:nakedret // FIXME try to refactor this in the future
641643
func convertActionData(aType ActionType, raw json.RawMessage) (ad Action_Data, err error) {
642644
switch aType {
643645
case CANCEL:
@@ -687,6 +689,7 @@ func convertActionData(aType ActionType, raw json.RawMessage) (ad Action_Data, e
687689
}
688690
}
689691

692+
//nolint:gosec // memory aliasing is used to convert from pointers to values and the other way
690693
func convertActions(zlog zerolog.Logger, agentID string, actions []model.Action) ([]Action, string) {
691694
var ackToken string
692695
sz := len(actions)

Diff for: internal/pkg/api/openapi_spec_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// or more contributor license agreements. Licensed under the Elastic License;
33
// you may not use this file except in compliance with the Elastic License.
44

5-
//nolint:dupl // don't care about repitition for tests
5+
//nolint:dupl,goconst // don't care about repitition for tests
66
package api
77

88
// Test json encoding/decoding for all req/resp items
@@ -227,6 +227,7 @@ func Test_UpgradeDetailsMetadata_Downloading(t *testing.T) {
227227
}}
228228

229229
for _, tc := range tests {
230+
tc := tc
230231
t.Run(tc.name, func(t *testing.T) {
231232
meta, err := tc.md.AsUpgradeMetadataDownloading()
232233
if tc.err == nil {
@@ -281,6 +282,7 @@ func Test_UpgradeDetailsMetadata_Failed(t *testing.T) {
281282
}}
282283

283284
for _, tc := range tests {
285+
tc := tc
284286
t.Run(tc.name, func(t *testing.T) {
285287
meta, err := tc.md.AsUpgradeMetadataFailed()
286288
if tc.err == nil {
@@ -336,6 +338,7 @@ func Test_UpgradeDetailsMetadata_Scheduled(t *testing.T) {
336338
}}
337339

338340
for _, tc := range tests {
341+
tc := tc
339342
t.Run(tc.name, func(t *testing.T) {
340343
meta, err := tc.md.AsUpgradeMetadataScheduled()
341344
if tc.err == nil {
@@ -378,6 +381,7 @@ func TestUpgradeDetailsSerialization(t *testing.T) {
378381
TargetVersion: "1.2.3",
379382
}}
380383
for _, d := range details {
384+
d := d
381385
t.Run(string(d.State), func(t *testing.T) {
382386
p, err := json.Marshal(d)
383387
require.NoError(t, err)

Diff for: internal/pkg/bulk/bulk_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"context"
1010
"encoding/json"
1111
"errors"
12-
"io/ioutil"
12+
"io"
1313
"net/http"
1414
"strconv"
1515
"sync"
@@ -101,7 +101,7 @@ func (m *mockBulkTransport) Perform(req *http.Request) (*http.Response, error) {
101101
Proto: "HTTP/1.1",
102102
ProtoMajor: 1,
103103
ProtoMinor: 1,
104-
Body: ioutil.NopCloser(&body),
104+
Body: io.NopCloser(&body),
105105
}
106106

107107
return resp, nil

Diff for: internal/pkg/bulk/helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package bulk
66

77
import (
88
"encoding/json"
9-
"io/ioutil"
9+
"io"
1010

1111
"github.com/elastic/fleet-server/v7/internal/pkg/es"
1212
"github.com/elastic/go-elasticsearch/v8/esapi"
@@ -41,7 +41,7 @@ func parseError(res *esapi.Response, log *zerolog.Logger) error {
4141

4242
if err := decoder.Decode(&e); err != nil {
4343
log.Error().Err(err).Msg("Cannot decode Elasticsearch error body")
44-
bodyBytes, readErr := ioutil.ReadAll(res.Body)
44+
bodyBytes, readErr := io.ReadAll(res.Body)
4545
if readErr != nil {
4646
log.Debug().Err(readErr).Msg("Error reading error response body from Elasticsearch")
4747
} else {

Diff for: internal/pkg/cache/cache.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// you may not use this file except in compliance with the Elastic License.
44

55
// Package cache implements an in-memory cache used to track API keys, actions, and artifacts.
6+
7+
//nolint:goconst // easier to read scoped keys if no constants are used
68
package cache
79

810
import (

Diff for: internal/pkg/dl/actions.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,29 @@ func FindAgentActions(ctx context.Context, bulker bulk.Bulk, minSeqNo, maxSeqNo
126126
return hitsToActions(res.Hits)
127127
}
128128

129-
func DeleteExpiredForIndex(ctx context.Context, index string, bulker bulk.Bulk, cleanupIntervalAfterExpired string) (count int64, err error) {
129+
func DeleteExpiredForIndex(ctx context.Context, index string, bulker bulk.Bulk, cleanupIntervalAfterExpired string) (int64, error) {
130130
params := map[string]interface{}{
131131
FieldExpiration: "now-" + cleanupIntervalAfterExpired,
132132
}
133133

134134
query, err := QueryDeleteExpiredActions.Render(params)
135135
if err != nil {
136-
return
136+
return 0, err
137137
}
138138

139139
res, err := bulker.Client().API.DeleteByQuery([]string{index}, bytes.NewReader(query),
140140
bulker.Client().API.DeleteByQuery.WithContext(ctx))
141141

142142
if err != nil {
143-
return
143+
return 0, err
144144
}
145145
defer res.Body.Close()
146146

147147
var esres es.DeleteByQueryResponse
148148

149149
err = json.NewDecoder(res.Body).Decode(&esres)
150150
if err != nil {
151-
return
151+
return 0, err
152152
}
153153

154154
if res.IsError() {
@@ -158,7 +158,7 @@ func DeleteExpiredForIndex(ctx context.Context, index string, bulker bulk.Bulk,
158158
zerolog.Ctx(ctx).Debug().Str("index", index).Msg(es.ErrIndexNotFound.Error())
159159
err = nil
160160
}
161-
return
161+
return 0, err
162162
}
163163
}
164164

Diff for: internal/pkg/dl/enrollment_api_key.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ func FindEnrollmentAPIKey(ctx context.Context, bulker bulk.Bulk, tmpl *dsl.Tmpl,
5151
return findEnrollmentAPIKey(ctx, bulker, FleetEnrollmentAPIKeys, tmpl, field, id)
5252
}
5353

54-
func findEnrollmentAPIKey(ctx context.Context, bulker bulk.Bulk, index string, tmpl *dsl.Tmpl, field string, id string) (rec model.EnrollmentAPIKey, err error) {
54+
func findEnrollmentAPIKey(ctx context.Context, bulker bulk.Bulk, index string, tmpl *dsl.Tmpl, field string, id string) (model.EnrollmentAPIKey, error) {
55+
var rec model.EnrollmentAPIKey
5556
res, err := SearchWithOneParam(ctx, bulker, tmpl, index, field, id)
5657
if err != nil {
57-
return
58+
return rec, err
5859
}
5960

6061
sz := len(res.Hits)

Diff for: internal/pkg/dl/enrollment_api_key_integration_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ func createRandomEnrollmentAPIKey(policyID string, active bool) model.Enrollment
3838

3939
}
4040

41-
func storeRandomEnrollmentAPIKey(ctx context.Context, bulker bulk.Bulk, index string, policyID string, active bool) (rec model.EnrollmentAPIKey, err error) {
42-
rec = createRandomEnrollmentAPIKey(policyID, active)
41+
func storeRandomEnrollmentAPIKey(ctx context.Context, bulker bulk.Bulk, index string, policyID string, active bool) (model.EnrollmentAPIKey, error) {
42+
rec := createRandomEnrollmentAPIKey(policyID, active)
4343

4444
body, err := json.Marshal(rec)
4545
if err != nil {
46-
return
46+
return rec, err
4747
}
4848
_, err = bulker.Create(ctx, index, rec.Id, body, bulk.WithRefresh())
4949
if err != nil {
50-
return
50+
return rec, err
5151
}
5252
return rec, err
5353
}

Diff for: internal/pkg/dl/migration_integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//go:build integration
66

7+
//nolint:goconst // disable duplicate checking
78
package dl
89

910
import (

Diff for: internal/pkg/dl/policies_leader.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func prepareSearchPolicyLeaders() (*dsl.Tmpl, error) {
3636
}
3737

3838
// SearchPolicyLeaders returns all the leaders for the provided policies
39+
//
40+
//nolint:nakedret // leader election is going to be removed
3941
func SearchPolicyLeaders(ctx context.Context, bulker bulk.Bulk, ids []string, opt ...Option) (leaders map[string]model.PolicyLeader, err error) {
4042
initSearchPolicyLeadersOnce.Do(func() {
4143
tmplSearchPolicyLeaders, err = prepareSearchPolicyLeaders()

Diff for: internal/pkg/es/info.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ type infoResponse struct {
2121
Error json.RawMessage `json:"error,omitempty"`
2222
}
2323

24-
func FetchESVersion(ctx context.Context, esCli *elasticsearch.Client) (version string, err error) {
24+
func FetchESVersion(ctx context.Context, esCli *elasticsearch.Client) (string, error) {
2525
res, err := esCli.Info(
2626
esCli.Info.WithContext(ctx),
2727
)
2828

2929
if err != nil {
30-
return
30+
return "", err
3131
}
3232
defer res.Body.Close()
3333

3434
var sres infoResponse
3535

3636
err = json.NewDecoder(res.Body).Decode(&sres)
3737
if err != nil {
38-
return
38+
return "", err
3939
}
4040

4141
// Check error
4242
err = TranslateError(res.StatusCode, sres.Error)
4343
if err != nil {
44-
return
44+
return "", err
4545
}
4646

4747
verStr := strings.TrimSpace(strings.TrimSuffix(strings.ToLower(sres.Version.Number), "-snapshot"))

Diff for: internal/pkg/file/delivery/delivery_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14-
"io/ioutil"
1514
"net/http"
1615
"strings"
1716
"testing"
@@ -285,7 +284,7 @@ func sendBodyBytes(body []byte) *http.Response { return sendBody(bytes.NewReade
285284
func sendBody(body io.Reader) *http.Response {
286285
return &http.Response{
287286
StatusCode: http.StatusOK,
288-
Body: ioutil.NopCloser(body),
287+
Body: io.NopCloser(body),
289288
Header: http.Header{
290289
"X-Elastic-Product": []string{"Elasticsearch"},
291290
"Content-Type": []string{"application/cbor"},

Diff for: internal/pkg/file/uploader/upload_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// or more contributor license agreements. Licensed under the Elastic License;
33
// you may not use this file except in compliance with the Elastic License.
44

5+
//nolint:goconst // disable contstants checks for tests
56
package uploader
67

78
import (

Diff for: internal/pkg/gcheckpt/checkpoint.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
// Package gcheckpt handles the fleet API's global_checkpoints operations
66
// checkpoints are used to track which actions, agetnc (docs in general) have been read based on the seqno received.
7+
8+
//nolint:nakedret // FIXME refactor without naked returns at a later date
79
package gcheckpt
810

911
import (

Diff for: internal/pkg/policy/policy_output_integration_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//go:build integration
66

7+
//nolint:goconst // disable constants checks for tests
78
package policy
89

910
import (

Diff for: internal/pkg/server/fleet.go

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ func (f *Fleet) runServer(ctx context.Context, cfg *config.Config) (err error) {
394394
// the bulker exits. If the bulker exits before the error group,
395395
// this will tear down the error group and g.Wait() will return.
396396
// Otherwise it will be a noop.
397+
//nolint:nakedret // small function is easy to track
397398
g.Go(func() (err error) {
398399
select {
399400
case err = <-errCh:

Diff for: internal/pkg/server/fleet_integration_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//go:build integration
66

7-
//nolint:dupl // don't care about repeating code
7+
//nolint:dupl,goconst // don't care about repeating code
88
package server
99

1010
import (
@@ -14,7 +14,6 @@ import (
1414
"errors"
1515
"fmt"
1616
"io"
17-
"io/ioutil"
1817
"net/http"
1918
"net/http/httptest"
2019
"path"
@@ -428,7 +427,7 @@ func TestServerUnauthorized(t *testing.T) {
428427
defer res.Body.Close()
429428
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
430429

431-
raw, _ := ioutil.ReadAll(res.Body)
430+
raw, _ := io.ReadAll(res.Body)
432431
var resp api.HTTPErrResp
433432
err = json.Unmarshal(raw, &resp)
434433
if err != nil {
@@ -457,7 +456,7 @@ func TestServerUnauthorized(t *testing.T) {
457456

458457
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
459458

460-
raw, _ := ioutil.ReadAll(res.Body)
459+
raw, _ := io.ReadAll(res.Body)
461460
var resp api.HTTPErrResp
462461
err = json.Unmarshal(raw, &resp)
463462
if err != nil {

Diff for: internal/pkg/testing/esutil/client.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package esutil
66

77
import (
88
"io"
9-
"io/ioutil"
109
"net/http"
1110
"strings"
1211
"testing"
@@ -48,7 +47,7 @@ func sendBodyString(body string) *http.Response {
4847
func sendBody(body io.Reader) *http.Response {
4948
return &http.Response{
5049
StatusCode: http.StatusOK,
51-
Body: ioutil.NopCloser(body),
50+
Body: io.NopCloser(body),
5251
Header: http.Header{
5352
"X-Elastic-Product": []string{"Elasticsearch"},
5453
"Content-Type": []string{"application/cbor"},

Diff for: internal/pkg/testing/esutil/esutil.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12-
"io/ioutil"
12+
"io"
1313
"net/http"
1414
"strings"
1515

@@ -85,7 +85,7 @@ func parseResponseError(res *esapi.Response) (*errorResponse, error) {
8585
// Read the original body content, in case if it was a error from the cloud response
8686
// {"ok":false,"message":"Unknown deployment."}
8787
// So we can log it
88-
body, err := ioutil.ReadAll(res.Body)
88+
body, err := io.ReadAll(res.Body)
8989
if err != nil {
9090
return nil, &ClientError{
9191
StatusCode: res.StatusCode,

0 commit comments

Comments
 (0)