Skip to content

Commit

Permalink
lint revive(deep-exit): refactor cmd/crowdsec
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jun 6, 2024
1 parent 114a966 commit d571452
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ issues:

- linters:
- revive
path: pkg/metabase/metabase.go
path: pkg/metabase/*.go

- linters:
- revive
Expand Down
109 changes: 32 additions & 77 deletions cmd/crowdsec/crowdsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H

if err := runParse(inputLineChan, inputEventChan, *parsers.Ctx, parsers.Nodes); err != nil {
// this error will never happen as parser.Parse is not able to return errors
log.Fatalf("starting parse error : %s", err)
return err
}

Expand Down Expand Up @@ -109,12 +108,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
bucketsTomb.Go(func() error {
defer trace.CatchPanic("crowdsec/runPour")

if err := runPour(inputEventChan, holders, buckets, cConfig); err != nil {
log.Fatalf("starting pour error : %s", err)
return err
}

return nil
return runPour(inputEventChan, holders, buckets, cConfig)
})
}
bucketWg.Done()
Expand All @@ -140,12 +134,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H
outputsTomb.Go(func() error {
defer trace.CatchPanic("crowdsec/runOutput")

if err := runOutput(inputEventChan, outputEventChan, buckets, *parsers.Povfwctx, parsers.Povfwnodes, apiClient); err != nil {
log.Fatalf("starting outputs error : %s", err)
return err
}

return nil
return runOutput(inputEventChan, outputEventChan, buckets, *parsers.Povfwctx, parsers.Povfwnodes, apiClient)
})
}
outputWg.Done()
Expand Down Expand Up @@ -198,89 +187,55 @@ func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub
log.Debugf("Shutting down crowdsec routines")

if err := ShutdownCrowdsecRoutines(); err != nil {
log.Fatalf("unable to shutdown crowdsec routines: %s", err)
return fmt.Errorf("unable to shutdown crowdsec routines: %w", err)

Check warning on line 190 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L190

Added line #L190 was not covered by tests
}

log.Debugf("everything is dead, return crowdsecTomb")

if dumpStates {
dumpParserState()
dumpOverflowState()
dumpBucketsPour()
os.Exit(0)
}

return nil
})
}

func dumpBucketsPour() {
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
if err != nil {
log.Fatalf("open: %s", err)
}

out, err := yaml.Marshal(leaky.BucketPourCache)
if err != nil {
log.Fatalf("marshal: %s", err)
}

b, err := fd.Write(out)
if err != nil {
log.Fatalf("write: %s", err)
}

log.Tracef("wrote %d bytes", b)
log.Debugf("Dumping parser+bucket states to %s", parser.DumpFolder)

if err := fd.Close(); err != nil {
log.Fatalf(" close: %s", err)
}
}
if err := dumpState(
filepath.Join(parser.DumpFolder, "parser-dump.yaml"),
parser.StageParseCache,
); err != nil {
return fmt.Errorf("while dumping parser state: %w", err)
}

Check warning on line 203 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L202-L203

Added lines #L202 - L203 were not covered by tests

func dumpParserState() {
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "parser-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
if err != nil {
log.Fatalf("open: %s", err)
}
if err := dumpState(
filepath.Join(parser.DumpFolder, "bucket-dump.yaml"),
bucketOverflows,
); err != nil {
return fmt.Errorf("while dumping bucket overflow state: %w", err)

Check warning on line 209 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L209

Added line #L209 was not covered by tests

out, err := yaml.Marshal(parser.StageParseCache)
if err != nil {
log.Fatalf("marshal: %s", err)
}

b, err := fd.Write(out)
if err != nil {
log.Fatalf("write: %s", err)
}
}

Check warning on line 211 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L211

Added line #L211 was not covered by tests

log.Tracef("wrote %d bytes", b)
if err := dumpState(
filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"),
leaky.BucketPourCache,
); err != nil {
return fmt.Errorf("while dumping bucket pour state: %w", err)
}

Check warning on line 218 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L217-L218

Added lines #L217 - L218 were not covered by tests
}

if err := fd.Close(); err != nil {
log.Fatalf(" close: %s", err)
}
return nil
})
}

func dumpOverflowState() {
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucket-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
if err != nil {
log.Fatalf("open: %s", err)
}
func dumpState(destPath string, obj any) error {
dir := filepath.Dir(destPath)

out, err := yaml.Marshal(bucketOverflows)
err := os.MkdirAll(dir, 0o755)
if err != nil {
log.Fatalf("marshal: %s", err)
return err

Check warning on line 230 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L230

Added line #L230 was not covered by tests
}

b, err := fd.Write(out)
out, err := yaml.Marshal(obj)
if err != nil {
log.Fatalf("write: %s", err)
return err

Check warning on line 235 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L235

Added line #L235 was not covered by tests
}

log.Tracef("wrote %d bytes", b)

if err := fd.Close(); err != nil {
log.Fatalf(" close: %s", err)
}
return os.WriteFile(destPath, out, 0o666)
}

func waitOnTomb() {
Expand Down

0 comments on commit d571452

Please sign in to comment.