Skip to content

Commit 9dc77f2

Browse files
authored
lint: fix nilaway warnings (part 10) (#3875)
1 parent ab356f8 commit 9dc77f2

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

cmd/crowdsec-cli/climetrics/list.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package climetrics
22

33
import (
4+
"cmp"
45
"encoding/json"
56
"fmt"
7+
"slices"
68

79
"github.com/fatih/color"
810
"github.com/jedib0t/go-pretty/v6/table"
911
"github.com/jedib0t/go-pretty/v6/text"
1012
"github.com/spf13/cobra"
1113

12-
"github.com/crowdsecurity/go-cs-lib/maptools"
13-
1414
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/args"
1515
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/cstable"
1616
)
@@ -25,15 +25,23 @@ func (cli *cliMetrics) list() error {
2525
var allMetrics []metricType
2626

2727
ms := NewMetricStore()
28-
for _, section := range maptools.SortedKeys(ms) {
29-
title, description := ms[section].Description()
28+
for sectionName, section := range ms {
29+
if section == nil {
30+
continue
31+
}
32+
title, description := section.Description()
3033
allMetrics = append(allMetrics, metricType{
31-
Type: section,
34+
Type: sectionName,
3235
Title: title,
3336
Description: description,
3437
})
3538
}
3639

40+
// consistent output order
41+
slices.SortFunc(allMetrics, func(a, b metricType) int {
42+
return cmp.Compare(a.Type, b.Type)
43+
})
44+
3745
outputFormat := cli.cfg().Cscli.Output
3846

3947
switch outputFormat {

cmd/crowdsec-cli/climetrics/store.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ func (ms metricStore) Format(out io.Writer, wantColor string, sections []string,
256256
switch outputFormat {
257257
case "human":
258258
for _, section := range maptools.SortedKeys(want) {
259-
want[section].Table(out, wantColor, noUnit, showEmpty)
259+
// always ok, but keep nilaway happy
260+
if sec, ok := want[section]; ok && sec != nil {
261+
sec.Table(out, wantColor, noUnit, showEmpty)
262+
}
260263
}
261264
case "json":
262265
x, err := json.MarshalIndent(want, "", " ")

cmd/notification-file/main.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,26 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
6464
JSONFormat: true,
6565
})
6666

67-
func (r *LogRotate) rotateLogs(cfg PluginConfig) {
67+
func (r *LogRotate) rotateLogs(cfg PluginConfig) error {
6868
// Rotate the log file
6969
err := r.rotateLogFile(cfg.LogPath, r.MaxFiles)
7070
if err != nil {
71-
logger.Error("Failed to rotate log file", "error", err)
71+
return err
7272
}
7373
// Reopen the FileWriter
7474
FileWriter.Close()
7575
FileWriter, err = os.OpenFile(cfg.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
7676
if err != nil {
77-
logger.Error("Failed to reopen log file", "error", err)
77+
return err
7878
}
7979
// Reset the file size
8080
FileInfo, err := FileWriter.Stat()
8181
if err != nil {
82-
logger.Error("Failed to get file info", "error", err)
82+
return err
8383
}
8484
FileSize = FileInfo.Size()
85+
86+
return nil
8587
}
8688

8789
func (r *LogRotate) rotateLogFile(logPath string, maxBackups int) error {
@@ -193,7 +195,9 @@ func WriteToFileWithCtx(ctx context.Context, cfg PluginConfig, log string) error
193195
if FileSize > int64(cfg.LogRotate.MaxSize)*1024*1024 && cfg.LogRotate.Enabled {
194196
logger.Debug("Rotating log file", "file", cfg.LogPath)
195197
// Rotate the log file
196-
cfg.LogRotate.rotateLogs(cfg)
198+
if err = cfg.LogRotate.rotateLogs(cfg); err != nil {
199+
logger.Error("Failed to rotate log file", "error", err)
200+
}
197201
}
198202
}
199203
return err

0 commit comments

Comments
 (0)