Skip to content

Commit d64ee2a

Browse files
authored
update lint configuration (#3667)
* lint: exhaustive * lint: nonamedreturns * update golangci-lint * lint
1 parent 43a72e2 commit d64ee2a

File tree

7 files changed

+71
-35
lines changed

7 files changed

+71
-35
lines changed

.github/workflows/go-tests-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ jobs:
5858
- name: golangci-lint
5959
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
6060
with:
61-
version: v2.2
61+
version: v2.3
6262
args: --issues-exit-code=1 --timeout 10m
6363
only-new-issues: false

.github/workflows/go-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,6 @@ jobs:
209209
- name: golangci-lint
210210
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
211211
with:
212-
version: v2.2
212+
version: v2.3
213213
args: --issues-exit-code=1 --timeout 10m
214214
only-new-issues: false

.golangci.yml

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ linters:
2222
- intrange # intrange is a linter to find places where for loops could make use of an integer range.
2323
- funcorder
2424

25+
# we allow named returns (to improve readability, in the function signatures)
26+
# but disallow implicit (or "naked") returns, to improve readability in the actual code.
27+
#
28+
# Implicit returns are still useful with deferred functions, in which case they can be //nolinted.
29+
# https://go.dev/wiki/CodeReviewComments#named-result-parameters
30+
- nonamedreturns
31+
2532
#
2633
# Recommended? (easy)
2734
#
2835

2936
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
3037
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occasions, where the check for the returned error can be omitted.
31-
- exhaustive # check exhaustiveness of enum switch statements
3238
- godot # Check if comments end in a period
3339
- gosec # (gas): Inspects source code for security problems
3440
- musttag # enforce field tags in (un)marshaled structs
@@ -227,6 +233,8 @@ linters:
227233
disabled: true
228234
- name: empty-lines
229235
disabled: true
236+
- name: enforce-switch-style
237+
disabled: true
230238
- name: error-naming
231239
disabled: true
232240
- name: flag-parameter
@@ -341,12 +349,6 @@ linters:
341349
- bodyclose
342350
text: response body must be closed
343351

344-
# named/naked returns are evil, with a single exception
345-
# https://go.dev/wiki/CodeReviewComments#named-result-parameters
346-
- linters:
347-
- nonamedreturns
348-
text: named return .* with type .* found
349-
350352
- linters:
351353
- revive
352354
path: pkg/leakybucket/manager_load.go
@@ -472,6 +474,28 @@ linters:
472474
- containedctx
473475
path: cmd/notification-file/main.go
474476
text: found a struct that contains a context.Context field
477+
478+
# migrate over time
479+
480+
- linters:
481+
- noctx
482+
text: "net.Listen must not be called"
483+
484+
- linters:
485+
- noctx
486+
text: "exec.Command must not be called"
487+
488+
- linters:
489+
- noctx
490+
text: "net.LookupAddr must not be called"
491+
492+
- linters:
493+
- noctx
494+
text: "net.Dial must not be called"
495+
496+
- linters:
497+
- noctx
498+
text: "net.LookupHost must not be called"
475499
paths:
476500
- pkg/yamlpatch/merge.go
477501
- pkg/yamlpatch/merge_test.go

cmd/crowdsec/win_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest,
3636
case <-tick:
3737

3838
case c := <-r:
39-
switch c.Cmd {
39+
switch c.Cmd { //nolint:exhaustive
4040
case svc.Interrogate:
4141
changes <- c.CurrentStatus
4242
case svc.Stop, svc.Shutdown:

pkg/acquisition/modules/docker/docker.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/url"
99
"regexp"
10+
"slices"
1011
"strconv"
1112
"strings"
1213
"time"
@@ -25,7 +26,6 @@ import (
2526

2627
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
2728
"github.com/crowdsecurity/crowdsec/pkg/types"
28-
"slices"
2929
)
3030

3131
var linesRead = prometheus.NewCounterVec(
@@ -36,18 +36,19 @@ var linesRead = prometheus.NewCounterVec(
3636
[]string{"source"})
3737

3838
type DockerConfiguration struct {
39-
CheckInterval string `yaml:"check_interval"`
40-
FollowStdout bool `yaml:"follow_stdout"`
41-
FollowStdErr bool `yaml:"follow_stderr"`
42-
Until string `yaml:"until"`
43-
Since string `yaml:"since"`
44-
DockerHost string `yaml:"docker_host"`
45-
ContainerName []string `yaml:"container_name"`
46-
ContainerID []string `yaml:"container_id"`
47-
ContainerNameRegexp []string `yaml:"container_name_regexp"`
48-
ContainerIDRegexp []string `yaml:"container_id_regexp"`
49-
UseContainerLabels bool `yaml:"use_container_labels"`
5039
configuration.DataSourceCommonCfg `yaml:",inline"`
40+
41+
CheckInterval string `yaml:"check_interval"`
42+
FollowStdout bool `yaml:"follow_stdout"`
43+
FollowStdErr bool `yaml:"follow_stderr"`
44+
Until string `yaml:"until"`
45+
Since string `yaml:"since"`
46+
DockerHost string `yaml:"docker_host"`
47+
ContainerName []string `yaml:"container_name"`
48+
ContainerID []string `yaml:"container_id"`
49+
ContainerNameRegexp []string `yaml:"container_name_regexp"`
50+
ContainerIDRegexp []string `yaml:"container_id_regexp"`
51+
UseContainerLabels bool `yaml:"use_container_labels"`
5152
}
5253

5354
type DockerSource struct {
@@ -350,7 +351,9 @@ func (d *DockerSource) OneShotAcquisition(ctx context.Context, out chan types.Ev
350351
evt.Line = l
351352
evt.Process = true
352353
evt.Type = types.LOG
354+
353355
out <- evt
356+
354357
d.logger.Debugf("Sent line to parsing: %+v", evt.Line.Raw)
355358
}
356359
}
@@ -389,19 +392,19 @@ func (d *DockerSource) CanRun() error {
389392
return nil
390393
}
391394

392-
func (d *DockerSource) getContainerTTY(ctx context.Context, containerId string) bool {
393-
containerDetails, err := d.Client.ContainerInspect(ctx, containerId)
395+
func (d *DockerSource) getContainerTTY(ctx context.Context, containerID string) bool {
396+
containerDetails, err := d.Client.ContainerInspect(ctx, containerID)
394397
if err != nil {
395398
return false
396399
}
397400

398401
return containerDetails.Config.Tty
399402
}
400403

401-
func (d *DockerSource) getContainerLabels(ctx context.Context, containerId string) map[string]interface{} {
402-
containerDetails, err := d.Client.ContainerInspect(ctx, containerId)
404+
func (d *DockerSource) getContainerLabels(ctx context.Context, containerID string) map[string]any {
405+
containerDetails, err := d.Client.ContainerInspect(ctx, containerID)
403406
if err != nil {
404-
return map[string]interface{}{}
407+
return map[string]any{}
405408
}
406409

407410
return parseLabels(containerDetails.Config.Labels)
@@ -466,7 +469,7 @@ func (d *DockerSource) EvalContainer(ctx context.Context, container dockerTypes.
466469
return nil
467470
}
468471

469-
labelsTypeCast, ok := parsedLabels["labels"].(map[string]interface{})
472+
labelsTypeCast, ok := parsedLabels["labels"].(map[string]any)
470473
if !ok {
471474
d.logger.Error("container has 'crowdsec.enable' label set to true but 'labels' is not a map")
472475
return nil
@@ -539,6 +542,7 @@ func (d *DockerSource) checkContainers(ctx context.Context, monitChan chan *Cont
539542
}
540543

541544
d.logger.Tracef("Reading logs from %d containers", len(d.runningContainerState))
545+
542546
return nil
543547
}
544548

@@ -669,14 +673,15 @@ func (d *DockerSource) StreamingAcquisition(ctx context.Context, out chan types.
669673
return d.WatchContainer(ctx, monitChan, deleteChan)
670674
}
671675

672-
func (d *DockerSource) Dump() interface{} {
676+
func (d *DockerSource) Dump() any {
673677
return d
674678
}
675679

676680
func ReadTailScanner(scanner *bufio.Scanner, out chan string, t *tomb.Tomb) error {
677681
for scanner.Scan() {
678682
out <- scanner.Text()
679683
}
684+
680685
return scanner.Err()
681686
}
682687

@@ -714,6 +719,7 @@ func (d *DockerSource) TailDocker(ctx context.Context, container *ContainerConfi
714719
if line == "" {
715720
continue
716721
}
722+
717723
l := types.Line{}
718724
l.Raw = line
719725
l.Labels = container.Labels

pkg/csplugin/hclog_adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type HCLogAdapter struct {
4242
}
4343

4444
func (h HCLogAdapter) Log(level hclog.Level, msg string, args ...interface{}) {
45-
switch level {
45+
switch level { //nolint:exhaustive
4646
case hclog.NoLevel:
4747
return
4848
case hclog.Trace:
@@ -140,7 +140,7 @@ func (h HCLogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Write
140140

141141
// level2logrus maps hclog levels to Logrus levels.
142142
func level2logrus(level hclog.Level) logrus.Level {
143-
switch level {
143+
switch level { //nolint:exhaustive
144144
case hclog.NoLevel:
145145
// Logrus does not have NoLevel, so use Info instead.
146146
return logrus.InfoLevel
@@ -160,7 +160,7 @@ func level2logrus(level hclog.Level) logrus.Level {
160160
}
161161

162162
func level2hclog(level logrus.Level) hclog.Level {
163-
switch level {
163+
switch level { //nolint:exhaustive
164164
case logrus.TraceLevel:
165165
return hclog.Trace
166166
case logrus.DebugLevel:

pkg/parser/runtime.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func SetTargetByName(target string, value string, evt *types.Event) bool {
5151
/*
5252
** According to current Event layout we only have to handle struct and map
5353
*/
54-
switch iter.Kind() {
54+
switch iter.Kind() { //nolint:exhaustive
5555
case reflect.Map:
5656
tmp := iter.MapIndex(reflect.ValueOf(f))
5757
/*if we're in a map and the field doesn't exist, the user wants to add it :) */
@@ -82,7 +82,8 @@ func SetTargetByName(target string, value string, evt *types.Event) bool {
8282
return false
8383
}
8484
}
85-
//now we should have the final member :)
85+
86+
// now we should have the final member :)
8687
if !iter.CanSet() {
8788
log.Errorf("'%s' can't be set", target)
8889
return false
@@ -119,6 +120,7 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
119120
//we have a few cases :
120121
//(meta||key) + (static||reference||expr)
121122
var value string
123+
122124
clog := n.Logger
123125

124126
for _, static := range statics {
@@ -131,6 +133,7 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
131133
clog.Warningf("failed to run RunTimeValue : %v", err)
132134
continue
133135
}
136+
134137
switch out := output.(type) {
135138
case string:
136139
value = out
@@ -151,7 +154,7 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
151154
}
152155

153156
if value == "" {
154-
//allow ParseDate to have empty input
157+
// allow ParseDate to have empty input
155158
if static.Method != "ParseDate" {
156159
clog.Debugf("Empty value for %s, skip.", printStaticTarget(static))
157160
continue
@@ -180,6 +183,7 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
180183
} else {
181184
clog.Debugf("method '%s' doesn't exist or plugin not initialized", static.Method)
182185
}
186+
183187
if !processed {
184188
clog.Debugf("method '%s' doesn't exist", static.Method)
185189
}
@@ -202,6 +206,7 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
202206
clog.Fatal("unable to process static : unknown target")
203207
}
204208
}
209+
205210
return nil
206211
}
207212

@@ -253,6 +258,7 @@ func stageidx(stage string, stages []string) int {
253258
return i
254259
}
255260
}
261+
256262
return -1
257263
}
258264

0 commit comments

Comments
 (0)