Skip to content

Commit

Permalink
Fix cscli explain when running from testenv (#2114)
Browse files Browse the repository at this point in the history
* Fix cscli explain when running from testenv
  • Loading branch information
AlteredCoder authored Mar 15, 2023
1 parent b451d19 commit e61a464
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/crowdsec-cli/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ func runExplain(cmd *cobra.Command, args []string) error {
}

var f *os.File
dir := os.TempDir()

// using empty string fallback to /tmp
dir, err := os.MkdirTemp("", "cscli_explain")
if err != nil {
return fmt.Errorf("couldn't create a temporary directory to store cscli explain result: %s", err)
}
tmpFile := ""
// we create a temporary log file if a log line/stdin has been provided
if logLine != "" || logFile == "-" {
Expand Down Expand Up @@ -129,9 +133,8 @@ func runExplain(cmd *cobra.Command, args []string) error {
return fmt.Errorf("no acquisition (--file or --dsn) provided, can't run cscli test")
}

cmdArgs := []string{"-c", ConfigFilePath, "-type", logType, "-dsn", dsn, "-dump-data", "./", "-no-api"}
cmdArgs := []string{"-c", ConfigFilePath, "-type", logType, "-dsn", dsn, "-dump-data", dir, "-no-api"}
crowdsecCmd := exec.Command(crowdsec, cmdArgs...)
crowdsecCmd.Dir = dir
output, err := crowdsecCmd.CombinedOutput()
if err != nil {
fmt.Println(string(output))
Expand Down Expand Up @@ -159,6 +162,10 @@ func runExplain(cmd *cobra.Command, args []string) error {

hubtest.DumpTree(*parserDump, *bucketStateDump, opts)

if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("unable to delete temporary directory '%s': %s", dir, err)
}

return nil
}

Expand Down

0 comments on commit e61a464

Please sign in to comment.