Skip to content

Commit 1533837

Browse files
authored
lint: nosprintfhostport, ifelsechain (#3838)
* lint: nosprintfhostport * lint: enable gocritic check "ifelsechain" * update lint config * lint
1 parent 0b9a68d commit 1533837

File tree

6 files changed

+40
-46
lines changed

6 files changed

+40
-46
lines changed

.golangci.yml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,19 @@ linters:
142142
- (*github.com/crowdsecurity/crowdsec/pkg/appsec.ReqDumpFilter).FilterHeaders
143143
- (*github.com/crowdsecurity/crowdsec/pkg/longpollclient.LongPollClient).Stop
144144

145+
errorlint:
146+
# Check whether fmt.Errorf uses the %w verb for formatting errors.
147+
# See the https://github.com/polyfloyd/go-errorlint for caveats.
148+
# Default: true
149+
errorf: false
150+
errorf-multi: true
151+
asserts: true
152+
comparison: true
153+
145154
gocritic:
146155
enable-all: true
147156
disabled-checks:
148157
- paramTypeCombine
149-
- ifElseChain
150158
- hugeParam
151159
- commentedOutCode
152160
- commentedOutImport
@@ -163,6 +171,10 @@ linters:
163171
- preferFprint
164172
- importShadow
165173

174+
settings:
175+
ifElseChain:
176+
minThreshold: 4
177+
166178
gomoddirectives:
167179
replace-allow-list:
168180
- golang.org/x/time/rate
@@ -297,10 +309,6 @@ linters:
297309
- -QF1003
298310
- -QF1012
299311

300-
wsl:
301-
# Allow blocks to end with comments
302-
allow-trailing-comment: true
303-
304312
exclusions:
305313
presets:
306314
- comments
@@ -323,13 +331,6 @@ linters:
323331

324332
# Will fix, easy but some thinking required
325333

326-
- linters:
327-
- errorlint
328-
text: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
329-
- linters:
330-
- nosprintfhostport
331-
text: host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf
332-
333334
# https://github.com/timakin/bodyclose
334335
- linters:
335336
- bodyclose

cmd/crowdsec/run_in_svc_windows.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,33 @@ func StartRunSvc() error {
3737
return runService(svcName)
3838
}
3939

40-
if flags.WinSvc == "Install" {
40+
switch flags.WinSvc {
41+
case "Install":
4142
err = installService(svcName, svcDescription)
4243
if err != nil {
4344
return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
4445
}
45-
} else if flags.WinSvc == "Remove" {
46+
case "Remove":
4647
err = removeService(svcName)
4748
if err != nil {
4849
return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
4950
}
50-
} else if flags.WinSvc == "Start" {
51+
case "Start":
5152
err = startService(svcName)
5253
if err != nil {
5354
return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
5455
}
55-
} else if flags.WinSvc == "Stop" {
56+
case "Stop":
5657
err = controlService(svcName, svc.Stop, svc.Stopped)
5758
if err != nil {
5859
return fmt.Errorf("failed to %s %s: %w", flags.WinSvc, svcName, err)
5960
}
60-
} else if flags.WinSvc == "" {
61+
case "":
6162
return WindowsRun()
62-
} else {
63+
default:
6364
return fmt.Errorf("Invalid value for winsvc parameter: %s", flags.WinSvc)
6465
}
66+
6567
return nil
6668
}
6769

pkg/csconfig/config.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type Config struct {
4545
Hub *LocalHubCfg `yaml:"-"`
4646
}
4747

48-
// NewConfig
4948
func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, string, error) {
5049
patcher := csyaml.NewPatcher(configFile, ".local")
5150
patcher.SetQuiet(quiet)
@@ -99,9 +98,7 @@ func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool
9998
return nil, "", err
10099
}
101100

102-
if err = cfg.loadCSCLI(); err != nil {
103-
return nil, "", err
104-
}
101+
cfg.loadCSCLI()
105102

106103
globalConfig = cfg
107104

pkg/csconfig/cscli.go

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

33
import (
4-
"fmt"
4+
"net"
5+
"strconv"
56
)
67

7-
/*cscli specific config, such as hub directory*/
88
type CscliCfg struct {
99
Output string `yaml:"output,omitempty"`
1010
Color string `yaml:"color,omitempty"`
@@ -20,18 +20,19 @@ type CscliCfg struct {
2020

2121
const defaultHubURLTemplate = "https://cdn-hub.crowdsec.net/crowdsecurity/%s/%s"
2222

23-
func (c *Config) loadCSCLI() error {
23+
func (c *Config) loadCSCLI() {
2424
if c.Cscli == nil {
2525
c.Cscli = &CscliCfg{}
2626
}
2727

2828
if c.Prometheus.ListenAddr != "" && c.Prometheus.ListenPort != 0 {
29-
c.Cscli.PrometheusUrl = fmt.Sprintf("http://%s:%d/metrics", c.Prometheus.ListenAddr, c.Prometheus.ListenPort)
29+
c.Cscli.PrometheusUrl = "http://" + net.JoinHostPort(
30+
c.Prometheus.ListenAddr,
31+
strconv.Itoa(c.Prometheus.ListenPort),
32+
) + "/metrics"
3033
}
3134

3235
if c.Cscli.HubURLTemplate == "" {
3336
c.Cscli.HubURLTemplate = defaultHubURLTemplate
3437
}
35-
36-
return nil
3738
}

pkg/csconfig/cscli_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
8-
"github.com/crowdsecurity/go-cs-lib/cstest"
97
)
108

119
func TestLoadCSCLI(t *testing.T) {
1210
tests := []struct {
13-
name string
14-
input *Config
15-
expected *CscliCfg
16-
expectedErr string
11+
name string
12+
input *Config
13+
expected *CscliCfg
1714
}{
1815
{
1916
name: "basic valid configuration",
@@ -40,12 +37,7 @@ func TestLoadCSCLI(t *testing.T) {
4037

4138
for _, tc := range tests {
4239
t.Run(tc.name, func(t *testing.T) {
43-
err := tc.input.loadCSCLI()
44-
cstest.RequireErrorContains(t, err, tc.expectedErr)
45-
if tc.expectedErr != "" {
46-
return
47-
}
48-
40+
tc.input.loadCSCLI()
4941
assert.Equal(t, tc.expected, tc.input.Cscli)
5042
})
5143
}

pkg/parser/runtime.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
160160
}
161161
}
162162

163-
if static.Method != "" {
163+
switch {
164+
case static.Method != "":
164165
processed := false
165166
/*still way too hackish, but : inject all the results in enriched, and */
166167
if enricherPlugin, ok := n.EnrichFunctions.Registered[static.Method]; ok {
@@ -186,22 +187,22 @@ func (n *Node) ProcessStatics(statics []ExtraField, event *types.Event) error {
186187
if !processed {
187188
clog.Debugf("method '%s' doesn't exist", static.Method)
188189
}
189-
} else if static.Parsed != "" {
190+
case static.Parsed != "":
190191
clog.Debugf(".Parsed[%s] = '%s'", static.Parsed, value)
191192
event.Parsed[static.Parsed] = value
192-
} else if static.Meta != "" {
193+
case static.Meta != "":
193194
clog.Debugf(".Meta[%s] = '%s'", static.Meta, value)
194195
event.Meta[static.Meta] = value
195-
} else if static.Enriched != "" {
196+
case static.Enriched != "":
196197
clog.Debugf(".Enriched[%s] = '%s'", static.Enriched, value)
197198
event.Enriched[static.Enriched] = value
198-
} else if static.TargetByName != "" {
199+
case static.TargetByName != "":
199200
if !SetTargetByName(static.TargetByName, value, event) {
200201
clog.Errorf("Unable to set value of '%s'", static.TargetByName)
201202
} else {
202203
clog.Debugf("%s = '%s'", static.TargetByName, value)
203204
}
204-
} else {
205+
default:
205206
clog.Fatal("unable to process static : unknown target")
206207
}
207208
}

0 commit comments

Comments
 (0)