diff --git a/cmd/csaf_downloader/config.go b/cmd/csaf_downloader/config.go index d8dbee1..2c18ebe 100644 --- a/cmd/csaf_downloader/config.go +++ b/cmd/csaf_downloader/config.go @@ -9,11 +9,9 @@ package main import ( - "fmt" - "log" "net/http" - "regexp" + "github.com/csaf-poc/csaf_distribution/v2/internal/filter" "github.com/csaf-poc/csaf_distribution/v2/internal/models" "github.com/csaf-poc/csaf_distribution/v2/internal/options" ) @@ -43,7 +41,7 @@ type config struct { Config string `short:"c" long:"config" description:"Path to config TOML file" value-name:"TOML-FILE" toml:"-"` - ignorePattern []*regexp.Regexp + ignorePattern filter.PatternMatcher } // configPaths are the potential file locations of the config file. @@ -79,27 +77,16 @@ func parseArgsConfig() ([]string, *config, error) { // ignoreFile returns true if the given URL should not be downloaded. func (cfg *config) ignoreURL(u string) bool { - for _, expr := range cfg.ignorePattern { - if expr.MatchString(u) { - return true - } - } - return false + return cfg.ignorePattern.Matches(u) } // compileIgnorePatterns compiles the configure patterns to be ignored. func (cfg *config) compileIgnorePatterns() error { - cfg.ignorePattern = make([]*regexp.Regexp, 0, len(cfg.IgnorePattern)) - for _, pattern := range cfg.IgnorePattern { - expr, err := regexp.Compile(pattern) - if err != nil { - return fmt.Errorf("invalid ignorepattern: %w", err) - } - if cfg.Verbose { - log.Printf("ignore advisories containing pattern %q\n", expr) - } - cfg.ignorePattern = append(cfg.ignorePattern, expr) + pm, err := filter.NewPatternMatcher(cfg.IgnorePattern) + if err != nil { + return err } + cfg.ignorePattern = pm return nil } diff --git a/internal/filter/filter.go b/internal/filter/filter.go new file mode 100644 index 0000000..bdc6afb --- /dev/null +++ b/internal/filter/filter.go @@ -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) +// Software-Engineering: 2023 Intevation GmbH + +// 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 +}