Skip to content

Commit b63e73f

Browse files
committed
refact dump.go
1 parent d555702 commit b63e73f

File tree

4 files changed

+17
-33
lines changed

4 files changed

+17
-33
lines changed

cmd/crowdsec/crowdsec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ func serveCrowdsec(ctx context.Context, parsers *parser.Parsers, cConfig *csconf
228228
log.Debugf("everything is dead, return crowdsecTomb")
229229

230230
if dumpStates {
231-
if err := dumpAllStates(); err != nil {
231+
log.Debugf("Dumping parser+bucket states to %s", dumpFolder)
232+
233+
if err := dumpAllStates(dumpFolder); err != nil {
232234
log.Fatal(err)
233235
}
234236

cmd/crowdsec/dump.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,38 @@ import (
55
"os"
66
"path/filepath"
77

8-
log "github.com/sirupsen/logrus"
98
"gopkg.in/yaml.v3"
109

1110
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
1211
"github.com/crowdsecurity/crowdsec/pkg/parser"
1312
)
1413

15-
func dumpAllStates() error {
16-
log.Debugf("Dumping parser+bucket states to %s", parser.DumpFolder)
14+
func dumpAllStates(dir string) error {
15+
err := os.MkdirAll(dir, 0o755)
16+
if err != nil {
17+
return err
18+
}
1719

18-
if err := dumpState(
19-
filepath.Join(parser.DumpFolder, "parser-dump.yaml"),
20-
parser.StageParseCache,
21-
); err != nil {
22-
return fmt.Errorf("while dumping parser state: %w", err)
20+
if err := dumpState(dir, "parser-dump.yaml", parser.StageParseCache); err != nil {
21+
return fmt.Errorf("dumping parser state: %w", err)
2322
}
2423

25-
if err := dumpState(
26-
filepath.Join(parser.DumpFolder, "bucket-dump.yaml"),
27-
bucketOverflows,
28-
); err != nil {
29-
return fmt.Errorf("while dumping bucket overflow state: %w", err)
24+
if err := dumpState(dir, "bucket-dump.yaml", bucketOverflows); err != nil {
25+
return fmt.Errorf("dumping bucket overflow state: %w", err)
3026
}
3127

32-
if err := dumpState(
33-
filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"),
34-
leaky.BucketPourCache,
35-
); err != nil {
36-
return fmt.Errorf("while dumping bucket pour state: %w", err)
28+
if err := dumpState(dir, "bucketpour-dump.yaml", leaky.BucketPourCache); err != nil {
29+
return fmt.Errorf("dumping bucket pour state: %w", err)
3730
}
3831

3932
return nil
4033
}
4134

42-
func dumpState(destPath string, obj any) error {
43-
dir := filepath.Dir(destPath)
44-
45-
err := os.MkdirAll(dir, 0o755)
46-
if err != nil {
47-
return err
48-
}
49-
35+
func dumpState(dir, name string, obj any) error {
5036
out, err := yaml.Marshal(obj)
5137
if err != nil {
5238
return err
5339
}
5440

55-
return os.WriteFile(destPath, out, 0o666)
41+
return os.WriteFile(filepath.Join(dir, name), out, 0o666)
5642
}

cmd/crowdsec/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
129129

130130
if dumpFolder != "" {
131131
parser.ParseDump = true
132-
parser.DumpFolder = dumpFolder
133132
leakybucket.BucketPourTrack = true
134133
dumpStates = true
135134
}

pkg/parser/runtime.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,7 @@ func stageidx(stage string, stages []string) int {
242242
return -1
243243
}
244244

245-
var (
246-
ParseDump bool
247-
DumpFolder string
248-
)
245+
var ParseDump bool
249246

250247
var (
251248
StageParseCache dumps.ParserResults = make(dumps.ParserResults)

0 commit comments

Comments
 (0)