mirror of
https://github.com/railwayapp/cli
synced 2026-04-21 14:07:23 +00:00
Exit 1 on error and various tweaks (#131)
* Exit 1 on error, and various tweaks * Fix logic
This commit is contained in:
parent
99f128ef12
commit
c1de3a0424
4 changed files with 21 additions and 12 deletions
10
cmd/init.go
10
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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
8
main.go
8
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue