Files
ss/internal/automation/match.go

43 lines
941 B
Go

package automation
import (
"fmt"
"regexp"
"strings"
)
// matchProxies filters proxies down to those satisfying every non-empty
// dimension of m. Returns an error if m.NamePattern is an invalid regexp.
func matchProxies(proxies []ProxyRef, m MatchRule) ([]ProxyRef, error) {
var namePattern *regexp.Regexp
if m.NamePattern != "" {
re, err := regexp.Compile(m.NamePattern)
if err != nil {
return nil, fmt.Errorf("invalid match.name-pattern %q: %w", m.NamePattern, err)
}
namePattern = re
}
var out []ProxyRef
for _, p := range proxies {
if len(m.Subscriptions) > 0 && !containsFold(m.Subscriptions, p.Subscription) {
continue
}
if namePattern != nil && !namePattern.MatchString(p.OriginalName) {
continue
}
out = append(out, p)
}
return out, nil
}
func containsFold(list []string, s string) bool {
for _, item := range list {
if strings.EqualFold(item, s) {
return true
}
}
return false
}