driftctl/main.go

93 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
2021-01-04 17:28:40 +00:00
"time"
"github.com/fatih/color"
gosentry "github.com/getsentry/sentry-go"
"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"
"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() {
2021-01-04 17:28:40 +00:00
os.Exit(run())
}
func run() int {
config.Init()
2021-02-10 13:25:07 +00:00
logger.Init()
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)
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 {
gosentry.CurrentHub().Recover(err)
2021-01-04 17:28:40 +00:00
flushSentry()
logrus.Fatalf("Captured panic: %s", err)
os.Exit(2)
}
flushSentry()
}
}()
if _, err := driftctlCmd.ExecuteC(); err != nil {
if _, isNotInSync := err.(cmderrors.InfrastructureNotInSync); isNotInSync {
return 1
}
2021-01-04 17:28:40 +00:00
if cmd.IsReportingEnabled(&driftctlCmd.Command) {
sentry.CaptureException(err)
}
_, _ = fmt.Fprintln(os.Stderr, color.RedString("%s", err))
2021-01-04 17:28:40 +00:00
return 1
}
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)
}
}
2021-01-04 17:28:40 +00:00
return 0
}
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")
}