Exit 1 on error and various tweaks (#131)

* Exit 1 on error, and various tweaks

* Fix logic
This commit is contained in:
Gregory Schier 2021-06-08 15:57:27 -07:00 committed by GitHub
parent 99f128ef12
commit c1de3a0424
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 12 deletions

View file

@ -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()

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}