-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from csaf-poc/download-ignore-pattern
Downloader: ignore advisories by given patterns
- Loading branch information
Showing
4 changed files
with
86 additions
and
9 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,42 @@ | ||
// This file is Free Software under the MIT License | ||
// without warranty, see README.md and LICENSES/MIT.txt for details. | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de> | ||
// Software-Engineering: 2023 Intevation GmbH <https://intevation.de> | ||
|
||
// Package filter implements helps to filter advisories. | ||
package filter | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// PatternMatcher is a list of regular expressions. | ||
type PatternMatcher []*regexp.Regexp | ||
|
||
// NewPatternMatcher compiles a new list of regular expression from | ||
// a given list of strings. | ||
func NewPatternMatcher(patterns []string) (PatternMatcher, error) { | ||
pm := make(PatternMatcher, 0, len(patterns)) | ||
for _, pattern := range patterns { | ||
expr, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid ignore pattern: %w", err) | ||
} | ||
pm = append(pm, expr) | ||
} | ||
return pm, nil | ||
} | ||
|
||
// Matches returns true if the given string matches any of the expressions. | ||
func (pm PatternMatcher) Matches(s string) bool { | ||
for _, expr := range pm { | ||
if expr.MatchString(s) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |