From b0c7f8498391abea46a63a2240df111fe5e9d15a Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 6 Jun 2024 14:34:30 +0200 Subject: [PATCH] lint revive(deep-exit): refactor cmd/crowdsec --- .golangci.yml | 4 ++ cmd/crowdsec/crowdsec.go | 109 ++++++++++++--------------------------- 2 files changed, 36 insertions(+), 77 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index fd237c95bcc..f074eafdc53 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -490,6 +490,10 @@ issues: - revive path: pkg/metabase/metabase.go + - linters: + - revive + path: pkg/metabase/container.go + - linters: - revive path: cmd/crowdsec-cli/copyfile.go diff --git a/cmd/crowdsec/crowdsec.go b/cmd/crowdsec/crowdsec.go index d226e3a5796..4f43f63a838 100644 --- a/cmd/crowdsec/crowdsec.go +++ b/cmd/crowdsec/crowdsec.go @@ -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 } @@ -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() @@ -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() @@ -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) } 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) + } -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) - 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) - } + } - 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) + } + } - 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 } - b, err := fd.Write(out) + out, err := yaml.Marshal(obj) if err != nil { - log.Fatalf("write: %s", err) + return err } - 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() {