From c1de3a0424b6798c62613cb18e5c8a57ebddfc4b Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 8 Jun 2021 15:57:27 -0700 Subject: [PATCH] Exit 1 on error and various tweaks (#131) * Exit 1 on error, and various tweaks * Fix logic --- cmd/init.go | 10 ++++++---- constants/version.go | 4 ++++ controller/user.go | 11 +++-------- main.go | 8 ++++++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 859faff..afaeacb 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -181,10 +181,12 @@ func (h *Handler) Init(ctx context.Context, req *entity.CommandRequest) error { return h.Link(ctx, req) } - isLoggedIn, _ := h.ctrl.IsLoggedIn(ctx) - - if !isLoggedIn { - return fmt.Errorf("%s\nRun %s", ui.RedText("Account require to init project"), ui.Bold("railway login")) + // Since init can be called by guests, ensure we can fetch a user first before calling. This prevents + // us accidentally creating a temporary (guest) project if we have a token locally but our remote + // session was deleted. + _, err := h.ctrl.GetUser(ctx) + if err != nil { + return fmt.Errorf("%s\nRun %s", ui.RedText("Account required to init project"), ui.Bold("railway login")) } selection, err := ui.PromptInit() diff --git a/constants/version.go b/constants/version.go index a53a4f1..d8e85f2 100644 --- a/constants/version.go +++ b/constants/version.go @@ -3,3 +3,7 @@ package constants const VersionDefault = "Piped into LDflags on build. You are probably running Railway CLI from source." var Version string = VersionDefault + +func IsDevVersion() bool { + return Version == VersionDefault +} diff --git a/controller/user.go b/controller/user.go index 6414ccb..23e66df 100644 --- a/controller/user.go +++ b/controller/user.go @@ -175,7 +175,7 @@ func (c *Controller) browserlessLogin(ctx context.Context) (*entity.User, error) url := getBrowserlessLoginURL(wordCode) - fmt.Printf("Your pairing code is: %s\n", wordCode) + fmt.Printf("Your pairing code is: %s\n", ui.MagentaText(wordCode)) fmt.Printf("To authenticate with Railway, please go to \n %s\n", url) token, err := c.pollForToken(ctx, wordCode) @@ -214,17 +214,12 @@ func (c *Controller) Login(ctx context.Context, isBrowserless bool) (*entity.Use } func (c *Controller) Logout(ctx context.Context) error { - // Logout by wiping user configs - userCfg, err := c.cfg.GetUserConfigs() - if err != nil { - return err - } - if userCfg.Token == "" { + if loggedIn, _ := c.IsLoggedIn(ctx); !loggedIn { fmt.Printf("🚪 %s\n", ui.YellowText("Already logged out")) return nil } - err = c.gtwy.Logout(ctx) + err := c.gtwy.Logout(ctx) if err != nil { return err } diff --git a/main.go b/main.go index 2577be5..09d45fe 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,14 @@ func addRootCmd(cmd *cobra.Command) *cobra.Command { func contextualize(fn entity.HandlerFunction, panicFn entity.PanicFunction) entity.CobraFunction { return func(cmd *cobra.Command, args []string) error { ctx := context.Background() + defer func() { + // Skip recover during development, so we can see the panic stack traces instead of going + // through the "send to Railway" flow and hiding the stack from the user + if constants.IsDevVersion() { + return + } + if r := recover(); r != nil { err := panicFn(ctx, fmt.Sprint(r), string(debug.Stack()), cmd.Name(), args) if err != nil { @@ -50,6 +57,7 @@ func contextualize(fn entity.HandlerFunction, panicFn entity.PanicFunction) enti if err != nil { // TODO: Make it *pretty* fmt.Println(err.Error()) + os.Exit(1) // Set non-success exit code on error } return nil }