From 05b54687b6e638fe2092d42d84d26d527e2b183d Mon Sep 17 00:00:00 2001 From: Laurence Jones Date: Fri, 26 Apr 2024 15:56:15 +0100 Subject: [PATCH] feat: support stdout in cscli support dump (#2939) * feat: support stdout in cscli support dump * fix: skip log.info if stdout * fix: handle errors by returning to runE instead --- cmd/crowdsec-cli/support.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/crowdsec-cli/support.go b/cmd/crowdsec-cli/support.go index a48edeeeb..737411e28 100644 --- a/cmd/crowdsec-cli/support.go +++ b/cmd/crowdsec-cli/support.go @@ -319,7 +319,7 @@ cscli support dump -f /tmp/crowdsec-support.zip `, Args: cobra.NoArgs, DisableAutoGenTag: true, - Run: func(_ *cobra.Command, _ []string) { + RunE: func(_ *cobra.Command, _ []string) error { var err error var skipHub, skipDB, skipCAPI, skipLAPI, skipAgent bool infos := map[string][]byte{ @@ -473,15 +473,19 @@ cscli support dump -f /tmp/crowdsec-support.zip err = zipWriter.Close() if err != nil { - log.Fatalf("could not finalize zip file: %s", err) + return fmt.Errorf("could not finalize zip file: %s", err) } + if outFile == "-" { + _, err = os.Stdout.Write(w.Bytes()) + return err + } err = os.WriteFile(outFile, w.Bytes(), 0o600) if err != nil { - log.Fatalf("could not write zip file to %s: %s", outFile, err) + return fmt.Errorf("could not write zip file to %s: %s", outFile, err) } - log.Infof("Written zip file to %s", outFile) + return nil }, }