-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.go
More file actions
67 lines (55 loc) · 1.72 KB
/
Copy pathnamespace.go
File metadata and controls
67 lines (55 loc) · 1.72 KB
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
58
59
60
61
62
63
64
65
66
67
package debugo
import (
"regexp"
"strings"
)
func (d *Debugger) matchNamespace() bool {
namespace := GetNamespace()
if namespace == "*" {
return true
}
debugList := strings.Split(namespace, ",")
// 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(d.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(d.namespace, inclusionPattern) {
return true // If an inclusion matches, return true
}
}
return false
}
func matchPattern(namespace, pattern string) bool {
if strings.HasSuffix(pattern, ":?") {
base := strings.TrimSuffix(pattern, ":?")
// Match exactly the base or the base followed by anything
regexPattern := "^" + regexp.QuoteMeta(base) + "(:.*)?$"
re, err := regexp.Compile(regexPattern)
if err != nil {
return false
}
return re.MatchString(namespace)
}
// replace '*' with '.*' for regex matching (.* matches any sequence of characters)
regexPattern := "^" + strings.ReplaceAll(pattern, "*", ".*") + "$"
re, err := regexp.Compile(regexPattern)
if err != nil {
return false
}
return re.MatchString(namespace)
}