Don't quit on "fatal" errors

This commit is contained in:
Christian Rocha 2020-05-22 14:03:21 -04:00 committed by Christian Muehlhaeuser
parent e069be7af9
commit ddef264606

View file

@ -2,6 +2,7 @@ package ui
import ( import (
"errors" "errors"
"fmt"
"github.com/charmbracelet/boba" "github.com/charmbracelet/boba"
"github.com/charmbracelet/boba/spinner" "github.com/charmbracelet/boba/spinner"
@ -27,7 +28,6 @@ func NewProgram(style string) *boba.Program {
// MESSAGES // MESSAGES
type fatalErrMsg error
type errMsg error type errMsg error
type newCharmClientMsg *charm.Client type newCharmClientMsg *charm.Client
type sshAuthErrMsg struct{} type sshAuthErrMsg struct{}
@ -120,7 +120,9 @@ func initialize(style string) func() (boba.Model, boba.Cmd) {
func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) { func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
m, ok := mdl.(model) m, ok := mdl.(model)
if !ok { if !ok {
return model{err: errors.New("could not perform assertion on model in update")}, boba.Quit return model{
err: errors.New("could not perform assertion on model in update"),
}, boba.Quit
} }
var ( var (
@ -132,6 +134,8 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
case boba.KeyMsg: case boba.KeyMsg:
switch msg.String() { switch msg.String() {
case "f":
m.err = errors.New("Fatal.")
case "q": case "q":
fallthrough fallthrough
case "esc": case "esc":
@ -167,10 +171,6 @@ func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
return m, getTerminalSize() return m, getTerminalSize()
} }
case fatalErrMsg:
m.err = msg
return m, boba.Quit
case errMsg: case errMsg:
m.err = msg m.err = msg
return m, nil return m, nil
@ -279,7 +279,7 @@ func view(mdl boba.Model) string {
} }
if m.err != nil { if m.err != nil {
return m.err.Error() + "\n" return fmt.Sprintf("\nError: %v\n\nPress q to exit.", m.err)
} }
var s string var s string
@ -316,14 +316,14 @@ func getTerminalSize() boba.Cmd {
func newCharmClient() boba.Msg { func newCharmClient() boba.Msg {
cfg, err := charm.ConfigFromEnv() cfg, err := charm.ConfigFromEnv()
if err != nil { if err != nil {
return fatalErrMsg(err) return errMsg(err)
} }
cc, err := charm.NewClient(cfg) cc, err := charm.NewClient(cfg)
if err == charm.ErrMissingSSHAuth { if err == charm.ErrMissingSSHAuth {
return sshAuthErrMsg{} return sshAuthErrMsg{}
} else if err != nil { } else if err != nil {
return fatalErrMsg(err) return errMsg(err)
} }
return newCharmClientMsg(cc) return newCharmClientMsg(cc)