Skip to content

Commit 980ef05

Browse files
committed
refact cmd/crowdsec: remove redundant global variable
1 parent 1df2c55 commit 980ef05

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

cmd/crowdsec/crowdsec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func startLPMetrics(ctx context.Context, cConfig *csconfig.Config, apiClient *ap
127127
aggregated = true
128128
}
129129

130-
if err := acquisition.GetMetrics(dataSources, aggregated); err != nil {
130+
if err := acquisition.GetMetrics(datasources, aggregated); err != nil {
131131
return fmt.Errorf("while fetching prometheus metrics for datasources: %w", err)
132132
}
133133
}
@@ -167,7 +167,7 @@ func runCrowdsec(
167167

168168
log.Info("Starting processing data")
169169

170-
if err := acquisition.StartAcquisition(ctx, dataSources, logLines, &acquisTomb); err != nil {
170+
if err := acquisition.StartAcquisition(ctx, datasources, logLines, &acquisTomb); err != nil {
171171
return fmt.Errorf("starting acquisition error: %w", err)
172172
}
173173

@@ -212,7 +212,7 @@ func serveCrowdsec(
212212
waitOnTomb()
213213
log.Debugf("Shutting down crowdsec routines")
214214

215-
if err := ShutdownCrowdsecRoutines(cancel, &g); err != nil {
215+
if err := ShutdownCrowdsecRoutines(cancel, &g, datasources); err != nil {
216216
return fmt.Errorf("unable to shutdown crowdsec routines: %w", err)
217217
}
218218

cmd/crowdsec/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ var (
3939

4040
flags Flags
4141

42-
// the state of acquisition
43-
dataSources []acquisitionTypes.DataSource
4442
// the state of the buckets
4543
holders []leakybucket.BucketFactory
4644

@@ -66,27 +64,29 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error {
6664
}
6765

6866
func LoadAcquisition(ctx context.Context, cConfig *csconfig.Config, hub *cwhub.Hub) ([]acquisitionTypes.DataSource, error) {
67+
var datasources []acquisitionTypes.DataSource
68+
6969
if flags.SingleFileType != "" && flags.OneShotDSN != "" {
7070
flags.Labels["type"] = flags.SingleFileType
7171

7272
ds, err := acquisition.LoadAcquisitionFromDSN(ctx, flags.OneShotDSN, flags.Labels, flags.Transform, hub)
7373
if err != nil {
7474
return nil, err
7575
}
76-
dataSources = append(dataSources, ds)
76+
datasources = append(datasources, ds)
7777
} else {
7878
dss, err := acquisition.LoadAcquisitionFromFiles(ctx, cConfig.Crowdsec, cConfig.Prometheus, hub)
7979
if err != nil {
8080
return nil, err
8181
}
82-
dataSources = dss
82+
datasources = dss
8383
}
8484

85-
if len(dataSources) == 0 {
85+
if len(datasources) == 0 {
8686
return nil, errors.New("no datasource enabled")
8787
}
8888

89-
return dataSources, nil
89+
return datasources, nil
9090
}
9191

9292
// LoadConfig returns a configuration parsed from configuration file

cmd/crowdsec/serve.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/crowdsecurity/go-cs-lib/csdaemon"
1717
"github.com/crowdsecurity/go-cs-lib/trace"
1818

19+
acquisitionTypes "github.com/crowdsecurity/crowdsec/pkg/acquisition/types"
1920
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
2021
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
2122
"github.com/crowdsecurity/crowdsec/pkg/cticlient/ctiexpr"
@@ -99,12 +100,12 @@ func waitErrGroup(g *errgroup.Group, timeout time.Duration) error {
99100
}
100101
}
101102

102-
func ShutdownCrowdsecRoutines(cancel context.CancelFunc, g *errgroup.Group) error {
103+
func ShutdownCrowdsecRoutines(cancel context.CancelFunc, g *errgroup.Group, datasources []acquisitionTypes.DataSource) error {
103104
var reterr error
104105

105106
log.Debugf("Shutting down crowdsec sub-routines")
106107

107-
if len(dataSources) > 0 {
108+
if len(datasources) > 0 {
108109
acquisTomb.Kill(nil)
109110
log.Debugf("waiting for acquisition to finish")
110111
drainChan(logLines)

0 commit comments

Comments
 (0)