Skip to content

Commit 6a8f7c2

Browse files
authored
modernize: replace legacy slice/map/range idioms with stdlib (#3658)
1 parent 373bf66 commit 6a8f7c2

File tree

21 files changed

+106
-112
lines changed

21 files changed

+106
-112
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ linters:
194194
min-complexity: 16
195195

196196
nlreturn:
197-
block-size: 5
197+
block-size: 6
198198

199199
nolintlint:
200200
require-explanation: false # don't require an explanation for nolint directives

pkg/acquisition/acquisition.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/crowdsecurity/crowdsec/pkg/cwversion/component"
2525
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
2626
"github.com/crowdsecurity/crowdsec/pkg/types"
27+
"maps"
2728
)
2829

2930
type DataSourceUnavailableError struct {
@@ -354,9 +355,7 @@ func copyEvent(evt types.Event, line string) types.Event {
354355
evtCopy.Line.Raw = line
355356
evtCopy.Line.Labels = make(map[string]string)
356357

357-
for k, v := range evt.Line.Labels {
358-
evtCopy.Line.Labels[k] = v
359-
}
358+
maps.Copy(evtCopy.Line.Labels, evt.Line.Labels)
360359

361360
return evtCopy
362361
}
@@ -388,8 +387,8 @@ func transform(transformChan chan types.Event, output chan types.Event, acquisTo
388387
case string:
389388
logger.Tracef("transform expression returned %s", v)
390389
output <- copyEvent(evt, v)
391-
case []interface{}:
392-
logger.Tracef("transform expression returned %v", v) //nolint:asasalint // We actually want to log the slice content
390+
case []any:
391+
logger.Tracef("transform expression returned %v", v) // We actually want to log the slice content
393392

394393
for _, line := range v {
395394
l, ok := line.(string)

pkg/acquisition/modules/cloudwatch/cloudwatch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
2323
"github.com/crowdsecurity/crowdsec/pkg/parser"
2424
"github.com/crowdsecurity/crowdsec/pkg/types"
25+
"slices"
2526
)
2627

2728
var openedStreams = prometheus.NewGaugeVec(
@@ -411,7 +412,7 @@ func (cw *CloudwatchSource) LogStreamManager(ctx context.Context, in chan LogStr
411412
// stream exists, but is dead, remove it from list
412413
if !stream.t.Alive() {
413414
cw.logger.Debugf("stream %s already exists, but is dead", newStream.StreamName)
414-
cw.monitoredStreams = append(cw.monitoredStreams[:idx], cw.monitoredStreams[idx+1:]...)
415+
cw.monitoredStreams = slices.Delete(cw.monitoredStreams, idx, idx+1)
415416

416417
if cw.metricsLevel != configuration.METRICS_NONE {
417418
openedStreams.With(prometheus.Labels{"group": newStream.GroupName}).Dec()

pkg/acquisition/modules/docker/docker.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
2727
"github.com/crowdsecurity/crowdsec/pkg/types"
28+
"slices"
2829
)
2930

3031
var linesRead = prometheus.NewCounterVec(
@@ -407,10 +408,8 @@ func (d *DockerSource) getContainerLabels(ctx context.Context, containerId strin
407408
}
408409

409410
func (d *DockerSource) EvalContainer(ctx context.Context, container dockerTypes.Container) *ContainerConfig {
410-
for _, containerID := range d.Config.ContainerID {
411-
if containerID == container.ID {
412-
return &ContainerConfig{ID: container.ID, Name: container.Names[0], Labels: d.Config.Labels, Tty: d.getContainerTTY(ctx, container.ID)}
413-
}
411+
if slices.Contains(d.Config.ContainerID, container.ID) {
412+
return &ContainerConfig{ID: container.ID, Name: container.Names[0], Labels: d.Config.Labels, Tty: d.getContainerTTY(ctx, container.ID)}
414413
}
415414

416415
for _, containerName := range d.Config.ContainerName {

pkg/acquisition/modules/docker/utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dockeracquisition
22

33
import (
4+
"slices"
45
"strings"
56
)
67

@@ -22,10 +23,8 @@ func parseKeyToMap(m map[string]interface{}, key string, value string) {
2223
return
2324
}
2425

25-
for i := range parts {
26-
if parts[i] == "" {
27-
return
28-
}
26+
if slices.Contains(parts, "") {
27+
return
2928
}
3029

3130
for i := 1; i < len(parts)-1; i++ {

pkg/acquisition/modules/http/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ func (sr *slowReader) Read(p []byte) (int, error) {
544544
func assertEvents(out chan types.Event, expected []string, errChan chan error) {
545545
readLines := []types.Event{}
546546

547-
for i := 0; i < len(expected); i++ {
547+
for range expected {
548548
select {
549549
case event := <-out:
550550
readLines = append(readLines, event)

pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"gopkg.in/tomb.v2"
1818

1919
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
20+
"maps"
2021
)
2122

2223
type LokiClient struct {
@@ -314,9 +315,7 @@ func (lc *LokiClient) Get(ctx context.Context, url string) (*http.Response, erro
314315

315316
func NewLokiClient(config Config) *LokiClient {
316317
headers := make(map[string]string)
317-
for k, v := range config.Headers {
318-
headers[k] = v
319-
}
318+
maps.Copy(headers, config.Headers)
320319
if config.Username != "" || config.Password != "" {
321320
headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(config.Username+":"+config.Password))
322321
}

pkg/acquisition/modules/syslog/internal/parser/rfc3164/perf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func BenchmarkParse(b *testing.B) {
5252
var err error
5353
for _, test := range tests {
5454
b.Run(string(test.input), func(b *testing.B) {
55-
for i := 0; i < b.N; i++ {
55+
for b.Loop() {
5656
r := NewRFC3164Parser(test.opts...)
5757
err = r.Parse(test.input)
5858
}

pkg/acquisition/modules/syslog/internal/parser/rfc5424/perf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func BenchmarkParse(b *testing.B) {
9393
var err error
9494
for _, test := range tests {
9595
b.Run(test.label, func(b *testing.B) {
96-
for i := 0; i < b.N; i++ {
96+
for b.Loop() {
9797
r := NewRFC5424Parser()
9898
err = r.Parse(test.input)
9999
}

pkg/acquisition/modules/victorialogs/internal/vlclient/vl_client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"gopkg.in/tomb.v2"
1919

2020
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
21+
"maps"
2122
)
2223

2324
type VLClient struct {
@@ -386,9 +387,7 @@ func (lc *VLClient) Get(ctx context.Context, url string) (*http.Response, error)
386387

387388
func NewVLClient(config Config) *VLClient {
388389
headers := make(map[string]string)
389-
for k, v := range config.Headers {
390-
headers[k] = v
391-
}
390+
maps.Copy(headers, config.Headers)
392391

393392
if config.Username != "" || config.Password != "" {
394393
headers["Authorization"] = "Basic " + base64.StdEncoding.EncodeToString([]byte(config.Username+":"+config.Password))

0 commit comments

Comments
 (0)