2020-12-09 15:31:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-01-04 17:28:40 +00:00
|
|
|
"time"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2021-02-09 19:03:35 +00:00
|
|
|
gosentry "github.com/getsentry/sentry-go"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/joho/godotenv"
|
2021-01-04 17:28:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/build"
|
|
|
|
"github.com/snyk/driftctl/logger"
|
|
|
|
"github.com/snyk/driftctl/pkg/cmd"
|
|
|
|
cmderrors "github.com/snyk/driftctl/pkg/cmd/errors"
|
2022-02-17 17:31:37 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/cmd/scan"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/config"
|
|
|
|
"github.com/snyk/driftctl/pkg/version"
|
|
|
|
"github.com/snyk/driftctl/sentry"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
_ = godotenv.Load(".env.local")
|
|
|
|
_ = godotenv.Load() // The Original .env
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-01-04 17:28:40 +00:00
|
|
|
os.Exit(run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() int {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
config.Init()
|
2021-02-10 13:25:07 +00:00
|
|
|
logger.Init()
|
2021-11-25 14:07:50 +00:00
|
|
|
build := build.Build{}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"isRelease": fmt.Sprintf("%t", build.IsRelease()),
|
|
|
|
"isUsageReportingEnabled": fmt.Sprintf("%t", build.IsUsageReportingEnabled()),
|
|
|
|
"version": version.Current(),
|
|
|
|
}).Debug("Build info")
|
|
|
|
|
|
|
|
driftctlCmd := cmd.NewDriftctlCmd(build)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
checkVersion := driftctlCmd.ShouldCheckVersion()
|
|
|
|
latestVersionChan := make(chan string)
|
|
|
|
if checkVersion {
|
|
|
|
go func() {
|
|
|
|
latestVersion := version.CheckLatest()
|
|
|
|
latestVersionChan <- latestVersion
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:28:40 +00:00
|
|
|
// Handle panic and log them to sentry if error reporting is enabled
|
|
|
|
defer func() {
|
|
|
|
if cmd.IsReportingEnabled(&driftctlCmd.Command) {
|
|
|
|
err := recover()
|
|
|
|
if err != nil {
|
2021-02-09 19:03:35 +00:00
|
|
|
gosentry.CurrentHub().Recover(err)
|
2021-01-04 17:28:40 +00:00
|
|
|
flushSentry()
|
|
|
|
logrus.Fatalf("Captured panic: %s", err)
|
2022-02-17 17:31:37 +00:00
|
|
|
os.Exit(scan.EXIT_ERROR)
|
2021-01-04 17:28:40 +00:00
|
|
|
}
|
|
|
|
flushSentry()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
if _, err := driftctlCmd.ExecuteC(); err != nil {
|
2021-01-27 18:12:25 +00:00
|
|
|
if _, isNotInSync := err.(cmderrors.InfrastructureNotInSync); isNotInSync {
|
2022-02-17 17:31:37 +00:00
|
|
|
return scan.EXIT_NOT_IN_SYNC
|
2021-01-27 18:12:25 +00:00
|
|
|
}
|
2021-01-04 17:28:40 +00:00
|
|
|
if cmd.IsReportingEnabled(&driftctlCmd.Command) {
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
}
|
2021-03-03 16:20:25 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, color.RedString("%s", err))
|
2022-02-17 17:31:37 +00:00
|
|
|
return scan.EXIT_ERROR
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if checkVersion {
|
|
|
|
newVersion := <-latestVersionChan
|
|
|
|
if newVersion != "" {
|
2022-01-30 09:50:20 +00:00
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "\n\nYour version of driftctl is outdated, please upgrade!")
|
2021-03-03 16:20:25 +00:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Current: %s; Latest: %s\n", version.Current(), newVersion)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 17:28:40 +00:00
|
|
|
|
2022-02-17 17:31:37 +00:00
|
|
|
return scan.EXIT_IN_SYNC
|
2021-01-04 17:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func flushSentry() {
|
2021-04-20 14:00:39 +00:00
|
|
|
ttl := 60 * time.Second
|
|
|
|
ok := gosentry.Flush(ttl)
|
|
|
|
logrus.WithField("timeout", ttl).WithField("success", ok).Debug("Flushed Sentry events")
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|