-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
79 lines (71 loc) · 1.43 KB
/
path.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
70
71
72
73
74
75
76
77
78
79
package pzip
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/bmatcuk/doublestar/v4"
)
type SkipPath struct {
Includes []string
Excludes []string
}
func (p SkipPath) SkipOnSlash(path string) bool {
if len(p.Includes) > 0 {
in := false
for _, pattern := range p.Includes {
ok, _ := doublestar.Match(pattern, path)
if ok {
in = true
break
}
}
if !in {
return true
}
}
for _, pattern := range p.Excludes {
ok, _ := doublestar.Match(pattern, path)
if ok {
return true
}
}
return false
}
func (p SkipPath) Skip(path string) bool {
if len(p.Includes) > 0 {
in := false
for _, pattern := range p.Includes {
ok, _ := doublestar.PathMatch(pattern, path)
if ok {
in = true
break
}
}
if !in {
return true
}
}
for _, pattern := range p.Excludes {
ok, _ := doublestar.PathMatch(pattern, path)
if ok {
return true
}
}
return false
}
func SetupSignalContext() context.Context {
shutdownHandler := make(chan os.Signal, 2)
ctx, cancel := context.WithCancel(context.Background())
signal.Notify(shutdownHandler, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
s := <-shutdownHandler
_, _ = fmt.Fprintf(os.Stderr, "\nReceived signal: %s. Stopping...\n", s.String())
_, _ = fmt.Fprintln(os.Stderr, "Send the signal again to force a shutdown.")
cancel()
<-shutdownHandler
os.Exit(1) // second signal. Exit directly.
}()
return ctx
}