-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch.go
69 lines (60 loc) · 1.57 KB
/
match.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
58
59
60
61
62
63
64
65
66
67
68
69
//go:build windows
// +build windows
package winproc
import "strings"
// A StringMatcher is a function that matches strings
type StringMatcher func(string) bool
// MatchAny returns true if any of the filters match the process.
//
// MatchAny returns true if no filters are provided.
func MatchAny(filters ...Filter) Filter {
return func(process Process) bool {
if len(filters) == 0 {
return true
}
for _, filter := range filters {
if filter(process) {
return true
}
}
return false
}
}
// MatchAll returns true if all of the filters match the process.
//
// MatchAll returns true if no filters are provided.
func MatchAll(filters ...Filter) Filter {
return func(process Process) bool {
for _, filter := range filters {
if !filter(process) {
return false
}
}
return true
}
}
// MatchID returns a filter that matches a process ID.
func MatchID(pid ID) Filter {
return func(process Process) bool {
return process.ID == pid
}
}
// MatchName returns a filter that matches a process name.
func MatchName(matcher StringMatcher) Filter {
return func(process Process) bool {
return matcher(process.Name)
}
}
// EqualsName returns a filter that matches a process name case-insensitively.
func EqualsName(name string) Filter {
return func(process Process) bool {
return strings.EqualFold(process.Name, name)
}
}
// ContainsName returns a filter that matches part of a process name.
func ContainsName(name string) Filter {
name = strings.ToLower(name)
return func(process Process) bool {
return strings.Contains(strings.ToLower(process.Name), name)
}
}