-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace.go
57 lines (46 loc) · 1.49 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package debugo
import (
"regexp"
"strings"
)
func (l *Debugger) matchNamespace() bool {
if l.forced || debug == "*" {
return true
}
debugList := strings.Split(debug, ",")
// Separate the exclusion and inclusion patterns
var exclusionPatterns []string
var inclusionPatterns []string
for _, pattern := range debugList {
pattern = strings.ToLower(strings.TrimSpace(pattern))
if strings.HasPrefix(pattern, "-") {
exclusionPatterns = append(exclusionPatterns, pattern[1:]) // Remove the "-" and store it as an exclusion
} else {
inclusionPatterns = append(inclusionPatterns, pattern)
}
}
// Check if any exclusion pattern matches the namespace
for _, exclusionPattern := range exclusionPatterns {
if matchPattern(l.namespace, exclusionPattern) {
return false // If an exclusion matches, return false immediately
}
}
// Check if any inclusion pattern matches the namespace
for _, inclusionPattern := range inclusionPatterns {
if matchPattern(l.namespace, inclusionPattern) {
return true // If an inclusion matches, return true
}
}
return false
}
func matchPattern(namespace, pattern string) bool {
// Replace '*' with '.*' for regex matching (.* matches any sequence of characters)
regexPattern := "^" + strings.ReplaceAll(pattern, "*", ".*") + "$"
// Compile the pattern into a regular expression
re, err := regexp.Compile(regexPattern)
if err != nil {
return false
}
// Check if the namespace matches the regular expression
return re.MatchString(namespace)
}