Skip to content

Commit 33495f0

Browse files
authored
Fix issues reported by golangci-lint (#1902)
* linter(unused): fix linter remarks Signed-off-by: Peter Motičák <[email protected]> * linter(staticcheck): Fix linter remarks Signed-off-by: Peter Motičák <[email protected]> * Create .golangci.yml file Signed-off-by: Peter Motičák <[email protected]> * linter(gosimple): Fix linter remarks Signed-off-by: Peter Motičák <[email protected]> * linter(errcheck): Fix linter remarks Signed-off-by: Peter Motičák <[email protected]> * linter(ineffassign): Fix linter remarks Signed-off-by: Peter Motičák <[email protected]> * Add exclude rule for staticcheck Signed-off-by: Peter Motičák <[email protected]> * linter(govet): Fix linter remarks These issues were concerning copying mutex value in various places. Signed-off-by: Peter Motičák <[email protected]> * Run go mod tidy Signed-off-by: Peter Motičák <[email protected]> * Add golangci-lint to Makefile, remove old linter Signed-off-by: Peter Motičák <[email protected]> * Fix govppmux plugin Signed-off-by: Peter Motičák <[email protected]> * Add golangci-lint to GitHub workflows Signed-off-by: Peter Motičák <[email protected]> * Cleanup Signed-off-by: Peter Motičák <[email protected]> Signed-off-by: Peter Motičák <[email protected]>
1 parent 6ba221d commit 33495f0

File tree

131 files changed

+558
-767
lines changed

Some content is hidden

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

131 files changed

+558
-767
lines changed

.github/workflows/ci.yml

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ jobs:
1818
run: pip install --user yamllint
1919
- name: Run yamllint
2020
run: ~/.local/bin/yamllint -c .yamllint.yml .
21+
golangci:
22+
name: lint
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/setup-go@v3
26+
with:
27+
go-version: 1.18
28+
- uses: actions/checkout@v3
29+
- name: golangci-lint
30+
uses: golangci/golangci-lint-action@v3
31+
with:
32+
version: v1.50
33+
args: --timeout 5m
2134
checkproto:
2235
name: check proto
2336
runs-on: ubuntu-latest

.golangci.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
issues:
3+
exclude-rules:
4+
- linters:
5+
- staticcheck
6+
text: "SA1019:|SA1021:"
7+
- path: tests/
8+
linters:
9+
- errcheck
10+
- path: _test\.go
11+
linters:
12+
- errcheck
13+
- path: clientv2/vpp/dbadapter/data_resync_db.go
14+
linters:
15+
- staticcheck
16+
text: "SA4004:"
17+
- path: plugins/kvscheduler/plugin_scheduler.go
18+
linters:
19+
- staticcheck
20+
text: "SA2001:"
21+
- path: plugins/netalloc/netalloc_plugin.go
22+
linters:
23+
- staticcheck
24+
text: "SA1021:"
25+
# disable issue output limits
26+
max-issues-per-linter: 0
27+
max-same-issues: 0

Makefile

+3-4
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,17 @@ ifndef gotestsumcmd
267267
endif
268268
@env CGO_ENABLED=0 go build -ldflags="-s -w" -o $(BUILD_DIR)/test2json cmd/test2json
269269

270-
LINTER := $(shell command -v gometalinter 2> /dev/null)
270+
LINTER := $(shell command -v golangci-lint --version 2> /dev/null)
271271

272272
get-linters:
273273
ifndef LINTER
274274
@echo "# installing linters"
275-
go install github.com/alecthomas/gometalinter@latest
276-
gometalinter --install
275+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.50.1
277276
endif
278277

279278
lint: get-linters ## Lint Go code
280279
@echo "# running code analysis"
281-
./scripts/static_analysis.sh golint vet
280+
golangci-lint run
282281

283282
format: ## Format Go code
284283
@echo "# formatting the code"

client/dynamic_config.go

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121

2222
// field proto name/json name constants (can't be changes to not break json/yaml compatibility with configurator.Config)
2323
const (
24-
// configName is Name of field in fake config root Message that hold the real config root
25-
configName = "config"
2624
// configGroupSuffix is field proto name suffix that all fields referencing config groups has
2725
configGroupSuffix = "Config"
2826
// repeatedFieldsSuffix is suffix added to repeated fields inside config group message

clientv2/linux/dbadapter/data_resync_db.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,13 @@ type keySet map[string] /*key*/ interface{} /*nil*/
333333
// It deletes obsolete keys if listKeys() (from constructor) function is not nil.
334334
func (dsl *DataResyncDSL) Send() vppclient.Reply {
335335

336-
for dsl.listKeys != nil {
336+
if dsl.listKeys != nil {
337337
toBeDeleted := keySet{}
338338

339339
// fill all known keys associated with the Linux network configuration:
340340
keys, err := dsl.listKeys(interfaces.ModelInterface.KeyPrefix())
341341
if err != nil {
342-
break
342+
return dsl.vppDataResync.Send()
343343
}
344344
appendKeys(&toBeDeleted, keys)
345345

@@ -351,8 +351,6 @@ func (dsl *DataResyncDSL) Send() vppclient.Reply {
351351
for delKey := range toBeDeleted {
352352
dsl.txn.Delete(delKey)
353353
}
354-
355-
break
356354
}
357355

358356
return dsl.vppDataResync.Send()

cmd/agentctl/cli/cli.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"runtime"
2121

2222
"github.com/docker/cli/cli/streams"
23-
"github.com/docker/docker/pkg/term"
23+
"github.com/moby/term"
2424
"go.ligato.io/cn-infra/v2/logging"
2525

2626
"go.ligato.io/vpp-agent/v3/cmd/agentctl/api"

cmd/agentctl/cli/cli_options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"io"
1919

2020
"github.com/docker/cli/cli/streams"
21-
"github.com/docker/docker/pkg/term"
21+
"github.com/moby/term"
2222

2323
"go.ligato.io/vpp-agent/v3/cmd/agentctl/client"
2424
)

cmd/agentctl/client/client.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"go.ligato.io/cn-infra/v2/logging/logrus"
3636
"google.golang.org/grpc"
3737
"google.golang.org/grpc/credentials"
38+
"google.golang.org/grpc/credentials/insecure"
3839

3940
"go.ligato.io/vpp-agent/v3/client"
4041
"go.ligato.io/vpp-agent/v3/client/remoteclient"
@@ -297,7 +298,7 @@ func (c *Client) negotiateAPIVersionPing(p *types.Version) {
297298
}
298299

299300
func connectGrpc(addr string, tc *tls.Config) (*grpc.ClientConn, error) {
300-
dialOpt := grpc.WithInsecure()
301+
dialOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
301302
if tc != nil {
302303
dialOpt = grpc.WithTransportCredentials(credentials.NewTLS(tc))
303304
}

cmd/agentctl/client/http.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"net"
1110
"net/http"
1211
"net/url"
@@ -165,13 +164,13 @@ func (c *Client) doRequest(ctx context.Context, req *http.Request) (serverRespon
165164
return serverResp, errors.Wrap(err, "error during connect")
166165
}
167166
if logrus.IsLevelEnabled(logrus.DebugLevel) {
168-
body, err := ioutil.ReadAll(resp.Body)
167+
body, err := io.ReadAll(resp.Body)
169168
if err != nil {
170169
logrus.Debugf("reading body failed: %v", err)
171170
} else {
172171
logrus.Debugf("body: %s", body)
173172
}
174-
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
173+
resp.Body = io.NopCloser(bytes.NewReader(body))
175174
}
176175
if resp != nil {
177176
serverResp.statusCode = resp.StatusCode
@@ -194,7 +193,7 @@ func (c *Client) checkResponseErr(serverResp serverResponse) error {
194193
R: serverResp.body,
195194
N: int64(bodyMax),
196195
}
197-
body, err = ioutil.ReadAll(bodyR)
196+
body, err = io.ReadAll(bodyR)
198197
if err != nil {
199198
return err
200199
}
@@ -232,10 +231,8 @@ func (c *Client) addHeaders(req *http.Request, headers headers) *http.Request {
232231
for k, v := range c.customHTTPHeaders {
233232
req.Header.Set(k, v)
234233
}
235-
if headers != nil {
236-
for k, v := range headers {
237-
req.Header[k] = v
238-
}
234+
for k, v := range headers {
235+
req.Header[k] = v
239236
}
240237
return req
241238
}
@@ -253,7 +250,7 @@ func encodeData(data interface{}) (*bytes.Buffer, error) {
253250
func ensureReaderClosed(response serverResponse) {
254251
if response.body != nil {
255252
// Drain up to 512 bytes and close the body to let the Transport reuse the connection
256-
_, _ = io.CopyN(ioutil.Discard, response.body, 512)
253+
_, _ = io.CopyN(io.Discard, response.body, 512)
257254
_ = response.body.Close()
258255
}
259256
}

cmd/agentctl/client/tlsconfig/tlsconfig.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,34 @@
1515
// Package tlsconfig provides more convenient way to create "tls.Config".
1616
//
1717
// Usage:
18-
// package main
1918
//
20-
// import "fmt"
21-
// import "go.ligato.io/vpp-agent/v3/cmd/agentctl/client/tlsconfig"
19+
// package main
2220
//
23-
// func main() {
24-
// tc, err := tlsconfig.New(
25-
// tlsconfig.CA("/path/to/ca.crt"),
26-
// tlsconfig.CertKey("/path/to/server.crt", "/path/to/server.key"),
27-
// )
21+
// import "fmt"
22+
// import "go.ligato.io/vpp-agent/v3/cmd/agentctl/client/tlsconfig"
2823
//
29-
// if err != nil {
30-
// fmt.Printf("Error while creating TLS config: %v\n", err)
31-
// return
32-
// }
33-
// fmt.Println("TLS config is ready to use")
24+
// func main() {
25+
// tc, err := tlsconfig.New(
26+
// tlsconfig.CA("/path/to/ca.crt"),
27+
// tlsconfig.CertKey("/path/to/server.crt", "/path/to/server.key"),
28+
// )
3429
//
35-
// // `tc` usage
36-
// }
30+
// if err != nil {
31+
// fmt.Printf("Error while creating TLS config: %v\n", err)
32+
// return
33+
// }
34+
// fmt.Println("TLS config is ready to use")
3735
//
36+
// // `tc` usage
37+
// }
38+
3839
package tlsconfig
3940

4041
import (
4142
"crypto/tls"
4243
"crypto/x509"
4344
"fmt"
44-
"io/ioutil"
45+
"os"
4546
)
4647

4748
// New returns tls.Config with all options applied.
@@ -70,7 +71,7 @@ func CA(path string) Option {
7071
config.RootCAs = x509.NewCertPool()
7172
}
7273

73-
cert, err := ioutil.ReadFile(path)
74+
cert, err := os.ReadFile(path)
7475
if err != nil {
7576
return err
7677
}

cmd/agentctl/commands/config.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ Value: {{protomulti .Notification.GetNotification}}
461461

462462
logrus.Debugf("Notification[%d]: %v",
463463
notif.NextIdx-1, notif.Notification)
464-
nextIdx = notif.NextIdx
465464

466465
if err := formatAsTemplate(cli.Out(), format, notif); err != nil {
467466
return err
@@ -687,13 +686,11 @@ func printHistoryTable(out io.Writer, txns kvs.RecordedTxns, withDetails bool) {
687686
resClr = tablewriter.FgGreenColor
688687
}
689688
if withDetails {
690-
if errs != nil {
691-
for _, e := range errs {
692-
if detail != "" {
693-
detail += "\n"
694-
}
695-
detail += fmt.Sprintf("%v", e.Error())
689+
for _, e := range errs {
690+
if detail != "" {
691+
detail += "\n"
696692
}
693+
detail += fmt.Sprintf("%v", e.Error())
697694
}
698695
if reasons := txnPendingReasons(txn); reasons != "" {
699696
if detail != "" {

cmd/agentctl/commands/dump.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ func printDumpTable(out io.Writer, dump []api.RecordedKVWithMetadata) {
220220
}
221221
}
222222
val = fmt.Sprintf("# %s\n%s", d.Value.ProtoReflect().Descriptor().FullName(), val)
223-
var row []string
224-
row = []string{
223+
row := []string{
225224
model,
226225
orig.String(),
227226
val,

cmd/agentctl/commands/formatter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ func agoTmpl(t time.Time) time.Duration {
140140

141141
func shortHumanDuration(d time.Duration) string {
142142
if seconds := int(d.Seconds()); seconds < -1 {
143-
return fmt.Sprintf("<invalid>")
143+
return "<invalid>"
144144
} else if seconds < 0 {
145-
return fmt.Sprintf("0s")
145+
return "0s"
146146
} else if seconds < 60 {
147147
return fmt.Sprintf("%ds", seconds)
148148
} else if minutes := int(d.Minutes()); minutes < 60 {

cmd/agentctl/commands/import.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"bytes"
1919
"context"
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"path"
2323
"strings"
2424
"time"
@@ -161,7 +161,7 @@ type keyVal struct {
161161
}
162162

163163
func parseImportFile(importFile string) (keyVals []keyVal, err error) {
164-
b, err := ioutil.ReadFile(importFile)
164+
b, err := os.ReadFile(importFile)
165165
if err != nil {
166166
return nil, fmt.Errorf("reading input file failed: %v", err)
167167
}

0 commit comments

Comments
 (0)