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>
This commit is contained in:
Shivam Sandbhor 2022-02-28 19:27:59 +05:30 committed by GitHub
parent 45c1075ae0
commit 0f5e922851
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
@ -51,7 +52,6 @@ cscli explain --dsn "file://myfile.log" --type nginx
defer f.Close()
_, err = f.WriteString(logLine)
if err != nil {
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)
}
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 == "" {
@ -108,3 +112,17 @@ cscli explain --dsn "file://myfile.log" --type nginx
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
}