Load file only once if specified twice, and bail earlier if type is unknown (#2419)
This commit is contained in:
parent
ff7acd3347
commit
f0cda0406b
|
@ -130,6 +130,19 @@ func UpdateRegexpCacheMetrics() {
|
||||||
|
|
||||||
func FileInit(fileFolder string, filename string, fileType string) error {
|
func FileInit(fileFolder string, filename string, fileType string) error {
|
||||||
log.Debugf("init (folder:%s) (file:%s) (type:%s)", fileFolder, filename, fileType)
|
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)
|
filepath := filepath.Join(fileFolder, filename)
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -137,13 +150,6 @@ func FileInit(fileFolder string, filename string, fileType string) error {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
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)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if strings.HasPrefix(scanner.Text(), "#") { // allow comments
|
if strings.HasPrefix(scanner.Text(), "#") { // allow comments
|
||||||
|
@ -156,13 +162,11 @@ func FileInit(fileFolder string, filename string, fileType string) error {
|
||||||
case "regex", "regexp":
|
case "regex", "regexp":
|
||||||
if fflag.Re2RegexpInfileSupport.IsEnabled() {
|
if fflag.Re2RegexpInfileSupport.IsEnabled() {
|
||||||
dataFileRe2[filename] = append(dataFileRe2[filename], re2.MustCompile(scanner.Text()))
|
dataFileRe2[filename] = append(dataFileRe2[filename], re2.MustCompile(scanner.Text()))
|
||||||
} else {
|
continue
|
||||||
dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
|
|
||||||
}
|
}
|
||||||
|
dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
|
||||||
case "string":
|
case "string":
|
||||||
dataFile[filename] = append(dataFile[filename], scanner.Text())
|
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
|
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
|
//Expr helpers
|
||||||
|
|
||||||
// func Get(arr []string, index int) string {
|
// func Get(arr []string, index int) string {
|
||||||
|
|
Loading…
Reference in a new issue