Pārlūkot izejas kodu

Warn when log file in explain command is large. (#1293)

* Warn when log file in explain command is large.

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Shivam Sandbhor 3 gadi atpakaļ
vecāks
revīzija
0f5e922851
1 mainītis faili ar 19 papildinājumiem un 1 dzēšanām
  1. 19 1
      cmd/crowdsec-cli/explain.go

+ 19 - 1
cmd/crowdsec-cli/explain.go

@@ -1,6 +1,7 @@
 package main
 package main
 
 
 import (
 import (
+	"bufio"
 	"fmt"
 	"fmt"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
@@ -51,7 +52,6 @@ cscli explain --dsn "file://myfile.log" --type nginx
 				defer f.Close()
 				defer f.Close()
 
 
 				_, err = f.WriteString(logLine)
 				_, err = f.WriteString(logLine)
-
 				if err != nil {
 				if err != nil {
 					log.Fatal(err)
 					log.Fatal(err)
 				}
 				}
@@ -63,6 +63,10 @@ cscli explain --dsn "file://myfile.log" --type nginx
 					log.Fatalf("unable to get absolue path of '%s', exiting", logFile)
 					log.Fatalf("unable to get absolue path of '%s', exiting", logFile)
 				}
 				}
 				dsn = fmt.Sprintf("file://%s", absolutePath)
 				dsn = fmt.Sprintf("file://%s", absolutePath)
+				lineCount := getLineCountForFile(absolutePath)
+				if lineCount > 100 {
+					log.Warnf("log file contains %d lines. This may take lot of resources.", lineCount)
+				}
 			}
 			}
 
 
 			if dsn == "" {
 			if dsn == "" {
@@ -108,3 +112,17 @@ cscli explain --dsn "file://myfile.log" --type nginx
 
 
 	return cmdExplain
 	return cmdExplain
 }
 }
+
+func getLineCountForFile(filepath string) int {
+	f, err := os.Open(filepath)
+	if err != nil {
+		log.Fatalf("unable to open log file %s", filepath)
+	}
+	defer f.Close()
+	lc := 0
+	fs := bufio.NewScanner(f)
+	for fs.Scan() {
+		lc++
+	}
+	return lc
+}