Load file only once if specified twice, and bail earlier if type is unknown (#2419)

This commit is contained in:
Laurence Jones 2023-10-13 22:34:57 +01:00 committed by GitHub
parent ff7acd3347
commit f0cda0406b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -130,6 +130,19 @@ func UpdateRegexpCacheMetrics() {
func FileInit(fileFolder string, filename string, fileType string) error {
log.Debugf("init (folder:%s) (file:%s) (type:%s)", fileFolder, filename, fileType)
if fileType == "" {
log.Debugf("ignored file %s%s because no type specified", fileFolder, filename)
return nil
}
ok, err := existsInFileMaps(filename, fileType)
if ok {
log.Debugf("ignored file %s%s because already loaded", fileFolder, filename)
return nil
}
if err != nil {
return err
}
filepath := filepath.Join(fileFolder, filename)
file, err := os.Open(filepath)
if err != nil {
@ -137,13 +150,6 @@ func FileInit(fileFolder string, filename string, fileType string) error {
}
defer file.Close()
if fileType == "" {
log.Debugf("ignored file %s%s because no type specified", fileFolder, filename)
return nil
}
if _, ok := dataFile[filename]; !ok {
dataFile[filename] = []string{}
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "#") { // allow comments
@ -156,13 +162,11 @@ func FileInit(fileFolder string, filename string, fileType string) error {
case "regex", "regexp":
if fflag.Re2RegexpInfileSupport.IsEnabled() {
dataFileRe2[filename] = append(dataFileRe2[filename], re2.MustCompile(scanner.Text()))
} else {
dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
continue
}
dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
case "string":
dataFile[filename] = append(dataFile[filename], scanner.Text())
default:
return fmt.Errorf("unknown data type '%s' for : '%s'", fileType, filename)
}
}
@ -172,6 +176,24 @@ func FileInit(fileFolder string, filename string, fileType string) error {
return nil
}
func existsInFileMaps(filename string, ftype string) (bool, error) {
ok := false
var err error
switch ftype {
case "regex", "regexp":
if fflag.Re2RegexpInfileSupport.IsEnabled() {
_, ok = dataFileRe2[filename]
} else {
_, ok = dataFileRegex[filename]
}
case "string":
_, ok = dataFile[filename]
default:
err = fmt.Errorf("unknown data type '%s' for : '%s'", ftype, filename)
}
return ok, err
}
//Expr helpers
// func Get(arr []string, index int) string {