forked from mszostok/codeowners-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
40 lines (33 loc) · 955 Bytes
/
main.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
package main
import (
"context"
"os"
"os/signal"
"syscall"
cmd "go.szostok.io/codeowners/cmd/codeowners"
)
func main() {
ctx, cancelFunc := WithStopContext(context.Background())
defer cancelFunc()
if err := cmd.RootCmd().ExecuteContext(ctx); err != nil {
// error is already handled by `cobra`, we don't want to log it here as we will duplicate the message.
// If needed, based on error type we can exit with different codes.
//nolint:gocritic
os.Exit(1)
}
}
// WithStopContext returns a copy of parent with a new Done channel. The returned
// context's Done channel is closed on of SIGINT or SIGTERM signals.
func WithStopContext(parent context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
case <-sigCh:
cancel()
}
}()
return ctx, cancel
}