-
Notifications
You must be signed in to change notification settings - Fork 160
/
main.go
101 lines (87 loc) · 2.53 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"os"
"time"
"github.com/fatih/color"
gosentry "github.com/getsentry/sentry-go"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/build"
"github.com/snyk/driftctl/logger"
"github.com/snyk/driftctl/pkg/cmd"
cmderrors "github.com/snyk/driftctl/pkg/cmd/errors"
"github.com/snyk/driftctl/pkg/cmd/scan"
"github.com/snyk/driftctl/pkg/config"
"github.com/snyk/driftctl/pkg/version"
"github.com/snyk/driftctl/sentry"
)
func init() {
_ = godotenv.Load(".env.local")
_ = godotenv.Load() // The Original .env
}
func main() {
os.Exit(run())
}
func run() int {
config.Init()
logger.Init()
build := build.Build{}
// Check whether driftCTL is run under Snyk CLI
isSnyk := config.IsSnyk()
logrus.WithFields(logrus.Fields{
"isRelease": fmt.Sprintf("%t", build.IsRelease()),
"isUsageReportingEnabled": fmt.Sprintf("%t", build.IsUsageReportingEnabled()),
"version": version.Current(),
"isSnyk": fmt.Sprintf("%t", isSnyk),
}).Debug("Build info")
// Enable colorization when driftctl is launched under snyk cli (piped)
if isSnyk {
color.NoColor = false
}
driftctlCmd := cmd.NewDriftctlCmd(build)
checkVersion := driftctlCmd.ShouldCheckVersion()
latestVersionChan := make(chan string)
if checkVersion {
go func() {
latestVersion := version.CheckLatest()
latestVersionChan <- latestVersion
}()
}
// Handle panic and log them to sentry if error reporting is enabled
defer func() {
if cmd.IsReportingEnabled(&driftctlCmd.Command) {
err := recover()
if err != nil {
gosentry.CurrentHub().Recover(err)
flushSentry()
logrus.Fatalf("Captured panic: %s", err)
os.Exit(scan.EXIT_ERROR)
}
flushSentry()
}
}()
if _, err := driftctlCmd.ExecuteC(); err != nil {
if _, isNotInSync := err.(cmderrors.InfrastructureNotInSync); isNotInSync {
return scan.EXIT_NOT_IN_SYNC
}
if cmd.IsReportingEnabled(&driftctlCmd.Command) {
sentry.CaptureException(err)
}
_, _ = fmt.Fprintln(os.Stderr, color.RedString("%s", err))
return scan.EXIT_ERROR
}
if checkVersion {
newVersion := <-latestVersionChan
if newVersion != "" {
_, _ = fmt.Fprintln(os.Stderr, "\n\nYour version of driftctl is outdated, please upgrade!")
_, _ = fmt.Fprintf(os.Stderr, "Current: %s; Latest: %s\n", version.Current(), newVersion)
}
}
return scan.EXIT_IN_SYNC
}
func flushSentry() {
ttl := 60 * time.Second
ok := gosentry.Flush(ttl)
logrus.WithField("timeout", ttl).WithField("success", ok).Debug("Flushed Sentry events")
}