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/cloudskiff/driftctl/build"
|
|
|
|
"github.com/cloudskiff/driftctl/logger"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/cmd"
|
2021-01-27 18:12:25 +00:00
|
|
|
cmderrors "github.com/cloudskiff/driftctl/pkg/cmd/errors"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/config"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/version"
|
|
|
|
"github.com/fatih/color"
|
2021-01-04 17:28:40 +00:00
|
|
|
"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"
|
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()
|
|
|
|
logger.Init(logger.GetConfig())
|
|
|
|
|
|
|
|
driftctlCmd := cmd.NewDriftctlCmd(build.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 {
|
|
|
|
sentry.CurrentHub().Recover(err)
|
|
|
|
flushSentry()
|
|
|
|
logrus.Fatalf("Captured panic: %s", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return 1
|
|
|
|
}
|
2021-01-04 17:28:40 +00:00
|
|
|
if cmd.IsReportingEnabled(&driftctlCmd.Command) {
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
fmt.Fprintln(os.Stderr, color.RedString("%s", err))
|
2021-01-04 17:28:40 +00:00
|
|
|
return 1
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if checkVersion {
|
|
|
|
newVersion := <-latestVersionChan
|
|
|
|
if newVersion != "" {
|
|
|
|
fmt.Println("\n\nYour version of driftctl is outdated, please upgrade !")
|
2021-01-05 10:11:44 +00:00
|
|
|
fmt.Printf("Current: %s; Latest: %s\n", version.Current(), newVersion)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 17:28:40 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func flushSentry() {
|
|
|
|
fmt.Print("Sending error report ...")
|
|
|
|
sentry.Flush(60 * time.Second)
|
|
|
|
fmt.Printf(" done, thank you %s\n", color.RedString("❤️"))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|