Skip to content

Commit a7fbedf

Browse files
authored
lint: fix nilaway warnings (part 6) (#3864)
1 parent fa0f832 commit a7fbedf

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ type Config struct {
4949
Limit int
5050
}
5151

52-
func updateURI(uri string, newStart time.Time) string {
53-
u, _ := url.Parse(uri)
52+
func updateURI(uri string, newStart time.Time) (string, error) {
53+
u, err := url.Parse(uri)
54+
if err != nil {
55+
return "", fmt.Errorf("invalid VictoriaLogs URL %q: %w", uri, err)
56+
}
57+
5458
queryParams := u.Query()
5559

5660
if !newStart.IsZero() {
@@ -61,7 +65,7 @@ func updateURI(uri string, newStart time.Time) string {
6165

6266
u.RawQuery = queryParams.Encode()
6367

64-
return u.String()
68+
return u.String(), nil
6569
}
6670

6771
func (lc *VLClient) SetTomb(t *tomb.Tomb) {
@@ -120,7 +124,7 @@ func (lc *VLClient) doQueryRange(ctx context.Context, uri string, c chan *Log, i
120124
resp, err := lc.Get(ctx, uri)
121125
if err != nil {
122126
if ok := lc.shouldRetry(); !ok {
123-
return fmt.Errorf("error querying range: %w", err)
127+
return fmt.Errorf("querying range: %w", err)
124128
}
125129

126130
lc.increaseTicker(ticker)
@@ -144,7 +148,7 @@ func (lc *VLClient) doQueryRange(ctx context.Context, uri string, c chan *Log, i
144148

145149
n, largestTime, err := lc.readResponse(ctx, resp, c)
146150
if err != nil {
147-
return err
151+
return fmt.Errorf("querying range: %w", err)
148152
}
149153

150154
if !infinite && n < lc.config.Limit {
@@ -165,7 +169,10 @@ func (lc *VLClient) doQueryRange(ctx context.Context, uri string, c chan *Log, i
165169
}
166170
}
167171

168-
uri = updateURI(uri, largestTime)
172+
uri, err = updateURI(uri, largestTime)
173+
if err != nil {
174+
return fmt.Errorf("querying range: %w", err)
175+
}
169176
}
170177
}
171178
}
@@ -177,13 +184,13 @@ func (lc *VLClient) readResponse(ctx context.Context, resp *http.Response, c cha
177184
var (
178185
finishedReading bool
179186
n int
180-
latestTs time.Time
187+
latestTS time.Time
181188
)
182189

183190
for !finishedReading {
184191
select {
185192
case <-ctx.Done():
186-
return n, latestTs, nil
193+
return n, latestTS, nil
187194
default:
188195
}
189196

@@ -198,9 +205,9 @@ func (lc *VLClient) readResponse(ctx context.Context, resp *http.Response, c cha
198205
// b can be != nil when EOF is returned, so we need to process it
199206
finishedReading = true
200207
} else if errors.Is(err, context.Canceled) {
201-
return n, latestTs, nil
208+
return n, latestTS, nil
202209
} else {
203-
return n, latestTs, fmt.Errorf("cannot read line in response: %w", err)
210+
return n, latestTS, fmt.Errorf("cannot read line in response: %w", err)
204211
}
205212
}
206213

@@ -222,12 +229,12 @@ func (lc *VLClient) readResponse(ctx context.Context, resp *http.Response, c cha
222229
lc.Logger.Tracef("Got response: %+v", logLine)
223230
c <- &logLine
224231

225-
if logLine.Time.After(latestTs) {
226-
latestTs = logLine.Time
232+
if logLine.Time.After(latestTS) {
233+
latestTS = logLine.Time
227234
}
228235
}
229236

230-
return n, latestTs, nil
237+
return n, latestTS, nil
231238
}
232239

233240
func (lc *VLClient) getURLFor(endpoint string, params map[string]string) string {

pkg/acquisition/modules/victorialogs/victorialogs_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
log "github.com/sirupsen/logrus"
1818
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1920
"gopkg.in/tomb.v2"
2021

2122
"github.com/crowdsecurity/go-cs-lib/cstest"
@@ -212,10 +213,10 @@ func TestConfigureDSN(t *testing.T) {
212213
}
213214

214215
if test.scheme != "" {
215-
url, _ := url.Parse(vlSource.Config.URL)
216-
if test.scheme != url.Scheme {
217-
t.Fatalf("Schema mismatch : %s != %s", test.scheme, url.Scheme)
218-
}
216+
url, err := url.Parse(vlSource.Config.URL)
217+
require.NoError(t, err)
218+
require.NotNil(t, url)
219+
require.Equal(t, test.scheme, url.Scheme)
219220
}
220221

221222
if test.waitForReady != 0 {

pkg/csplugin/broker.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,22 @@ func (pb *PluginBroker) loadNotificationPlugin(ctx context.Context, name string,
377377
}
378378

379379
func (pb *PluginBroker) tryNotify(ctx context.Context, pluginName, message string) error {
380-
timeout := pb.pluginConfigByName[pluginName].TimeOut
380+
// config guard
381+
pc, ok := pb.pluginConfigByName[pluginName]
382+
if !ok {
383+
return fmt.Errorf("plugin %q: config not found", pluginName)
384+
}
385+
386+
timeout := pc.TimeOut
381387
ctxTimeout, cancel := context.WithTimeout(ctx, timeout)
382388

383389
defer cancel()
384390

385-
plugin := pb.notificationPluginByName[pluginName]
391+
// plugin guard
392+
plugin, ok := pb.notificationPluginByName[pluginName]
393+
if !ok || plugin == nil {
394+
return fmt.Errorf("plugin %q: notifier not registered", pluginName)
395+
}
386396

387397
_, err := plugin.Notify(
388398
ctxTimeout,

pkg/hubtest/helpers.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package hubtest
22

33
import (
4+
"errors"
5+
"fmt"
46
"path/filepath"
57
)
68

79
func basename(params ...any) (any, error) {
8-
s := params[0].(string)
10+
// keep nilaway happy
11+
if len(params) == 0 || params[0] == nil {
12+
return "", errors.New("basename: missing argument")
13+
}
14+
15+
// keep forcetypeassert happy
16+
s, ok := params[0].(string)
17+
if !ok {
18+
return "", fmt.Errorf("basename: want string, got %T", params[0])
19+
}
20+
921
return filepath.Base(s), nil
1022
}

0 commit comments

Comments
 (0)