-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading patterns from file via -pattern-file option.
- Loading branch information
Showing
4 changed files
with
83 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package util | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
func loadPatternsFromFile(path string) ([]*regexp.Regexp, error) { | ||
if path == "" { | ||
return nil, nil | ||
} | ||
path, err := filepath.Abs(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("pattern file path: %w", err) | ||
} | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("opening pattern file: %w", err) | ||
} | ||
defer func() { | ||
if e := file.Close(); e != nil && err == nil { | ||
err = e | ||
} | ||
}() | ||
|
||
var patterns []*regexp.Regexp | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
pattern := regexp.MustCompile(scanner.Text()) | ||
patterns = append(patterns, pattern) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return nil, fmt.Errorf("parsing pattern file: %w", err) | ||
} | ||
|
||
return patterns, nil | ||
} |