find.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package find provides utilities for matching names against regular expressions.
  14. package find
  15. import (
  16. "fmt"
  17. "regexp"
  18. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. )
  20. // Matcher represents a pattern matcher that uses regular expressions to match names.
  21. type Matcher struct {
  22. re *regexp.Regexp
  23. }
  24. // New creates a new Matcher using the provided FindName configuration.
  25. func New(findName esv1.FindName) (*Matcher, error) {
  26. cmp, err := regexp.Compile(findName.RegExp)
  27. if err != nil {
  28. return nil, fmt.Errorf("could not compile find.name.regexp [%s]: %w", findName.RegExp, err)
  29. }
  30. return &Matcher{
  31. re: cmp,
  32. }, nil
  33. }
  34. // MatchName checks if the given name matches the configured regular expression pattern.
  35. func (m *Matcher) MatchName(name string) bool {
  36. return m.re.MatchString(name)
  37. }