Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Explain] Ignore blank lines as crowdsec will anyways #2630

Merged
43 changes: 29 additions & 14 deletions cmd/crowdsec-cli/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
)

var (
dir string
LaurenceJJones marked this conversation as resolved.
Show resolved Hide resolved
)

func GetLineCountForFile(filepath string) (int, error) {
f, err := os.Open(filepath)
if err != nil {
return 0, err
}
defer f.Close()
lc := 0
fs := bufio.NewScanner(f)
for fs.Scan() {
lc++
fs := bufio.NewReader(f)
for {
input, err := fs.ReadBytes('\n')
if len(input) > 1 {
lc++
}
if err != nil && err == io.EOF {
break
}
}
return lc, nil
}
Expand Down Expand Up @@ -95,7 +105,7 @@ func runExplain(cmd *cobra.Command, args []string) error {
var f *os.File

// using empty string fallback to /tmp
dir, err := os.MkdirTemp("", "cscli_explain")
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)
}
Expand All @@ -121,6 +131,9 @@ func runExplain(cmd *cobra.Command, args []string) error {
if err != nil && err == io.EOF {
break
}
if len(input) <= 1 {
continue
}
_, err = f.Write(input)
if err != nil {
errCount++
Expand All @@ -145,6 +158,10 @@ func runExplain(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
log.Debug("line count: ", lineCount)
if lineCount == 0 {
return fmt.Errorf("the log file is empty: %s", absolutePath)
}
if lineCount > 100 {
log.Warnf("The log file contains %d lines. This may take a lot of resources.", lineCount)
}
Expand All @@ -166,12 +183,6 @@ func runExplain(cmd *cobra.Command, args []string) error {
return fmt.Errorf("fail to run crowdsec for test: %v", err)
}

// rm the temporary log file if only a log line/stdin was provided
if tmpFile != "" {
if err := os.Remove(tmpFile); err != nil {
return fmt.Errorf("unable to remove tmp log file '%s': %+v", tmpFile, err)
}
}
parserDumpFile := filepath.Join(dir, hubtest.ParserResultFileName)
bucketStateDumpFile := filepath.Join(dir, hubtest.BucketPourResultFileName)

Expand All @@ -187,10 +198,6 @@ 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 All @@ -210,6 +217,14 @@ tail -n 5 myfile.log | cscli explain --type nginx -f -
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: runExplain,
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if _, err := os.Stat(dir); !os.IsNotExist(err) {
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("unable to delete temporary directory '%s': %s", dir, err)
}
}
return nil
},
}

flags := cmdExplain.Flags()
Expand Down
Loading