Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Show error along with message and quit if applicable #87

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ContainerModel struct {
err error
flagKey string
flagsClient flags.Client
quitMsg string
quitting bool
steps []tea.Model
}
Expand All @@ -49,13 +50,20 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Enter):
switch m.currentStep {
case createFlagStep:
updated, _ := m.steps[createFlagStep].Update(msg)
updated, cmd := m.steps[createFlagStep].Update(msg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delegates the tea.Quit command. Seems cleaner?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels like we should leverage that functionality as much as possible!

if model, ok := updated.(createFlagModel); ok {
if model.err != nil {
m.err = model.err
if model.quitting {
m.quitMsg = model.quitMsg
m.quitting = true

return m, cmd
}

return m, nil
}

m.flagKey = model.flagKey
m.currentStep += 1
}
Expand All @@ -77,24 +85,33 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m ContainerModel) View() string {
if m.quitting {
return ""
// TODO: remove after creating more steps
if m.currentStep > createFlagStep {
return fmt.Sprintf("created flag %s", m.flagKey)
}

out := fmt.Sprintf("\nStep %d of %d\n"+m.steps[m.currentStep].View(), m.currentStep+1, len(m.steps))
if m.err != nil {
return lipgloss.
if m.quitting {
out := m.quitMsg + "\n\n"
out += m.err.Error()

return lipgloss.
NewStyle().
SetString(out).
Render() + "\n"
}

// show error and stay on the same step
out += "\n" + lipgloss.
NewStyle().
Foreground(lipgloss.Color("#eb4034")).
SetString(m.err.Error()).
Render()
}
Render() + "\n"

// TODO: remove after creating more steps
if m.currentStep > createFlagStep {
return fmt.Sprintf("created flag %s", m.flagKey)
return out
}

return fmt.Sprintf("\nStep %d of %d\n"+m.steps[m.currentStep].View(), m.currentStep+1, len(m.steps))
return out
}

type keyMap struct {
Expand Down
23 changes: 22 additions & 1 deletion internal/quickstart/create_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package quickstart

import (
"context"
"encoding/json"
"fmt"

"github.com/charmbracelet/bubbles/key"
Expand All @@ -17,10 +18,12 @@ import (
const defaultFlagName = "my new flag"

type createFlagModel struct {
client flags.Client
err error
flagKey string
flagName string
client flags.Client
quitMsg string
quitting bool
textInput textinput.Model
}

Expand Down Expand Up @@ -69,6 +72,24 @@ func (m createFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)
if err != nil {
m.err = err
// TODO: we may want a more robust error type so we don't need to do this
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we add more steps I'll refactor this.

var e struct {
Code string `json:"code"`
Message string `json:"message"`
}
_ = json.Unmarshal([]byte(m.err.Error()), &e)
switch {
case e.Code == "unauthorized":
m.quitting = true
m.quitMsg = "Your API key is unauthorized. Try another API key or speak to a LaunchDarkly account administrator."

return m, tea.Quit
case e.Code == "forbidden":
m.quitting = true
m.quitMsg = "You lack access to complete this action. Try authenticating with elevated access or speak to a LaunchDarkly account administrator."

return m, tea.Quit
}

return m, nil
}
Expand Down
Loading