forked from cloudquery/cloudquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (62 loc) · 1.47 KB
/
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
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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"github.com/cloudquery/cloudquery/cli/cmd"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func executeRootCmdWithContext() error {
// trap Ctrl+C and other signals, then call cancel on the context
ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
defer func() {
signal.Stop(c)
}()
var gotSignal os.Signal
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
select {
case gotSignal = <-c:
cancel()
case <-ctx.Done():
}
}()
err := cmd.NewCmdRoot().ExecuteContext(ctx)
cancel()
wg.Wait()
if gotSignal != nil && err != nil {
err = fmt.Errorf("received %v signal from OS: %w", gotSignal.String(), err)
} else if gotSignal != nil {
err = fmt.Errorf("received %v signal from OS", gotSignal.String())
}
return err
}
func main() {
exitCode := 0
defer func() {
err := recover()
if err != nil {
originalMessage := fmt.Sprintf("panic: %v\n\n%s", err, string(debug.Stack()))
sentry.CurrentHub().CaptureMessage(originalMessage)
panic(err)
}
os.Exit(exitCode)
}()
defer cmd.CloseLogFile()
// This ensures we don't print anything until logging is configured
log.Logger = log.Level(zerolog.Disabled)
if err := executeRootCmdWithContext(); err != nil {
log.Error().Err(err).Msg("exiting with error")
exitCode = 1
}
}