Skip to content

Commit

Permalink
feat: Show error along with message and quit if applicable (#87)
Browse files Browse the repository at this point in the history
Show error along with message and quit if necessary
  • Loading branch information
dbolson authored Mar 29, 2024
1 parent d0b2f61 commit f076a59
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
2 changes: 0 additions & 2 deletions cmd/members/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"ldcli/internal/members"
)

const defaultRole = "reader"

func NewInviteCmd(client members.Client) (*cobra.Command, error) {
cmd := &cobra.Command{
Args: validators.Validate(),
Expand Down
40 changes: 29 additions & 11 deletions internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ContainerModel struct {
err error
flagKey string
flagsClient flags.Client
quitMsg string
quitting bool
sdk sdkDetail
steps []tea.Model
Expand Down Expand Up @@ -52,12 +53,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)
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 Down Expand Up @@ -86,24 +95,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 > chooseSDKStep {
return fmt.Sprintf("created flag %s\nselected the %s SDK", m.flagKey, m.sdk.DisplayName)
}

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 > chooseSDKStep {
return fmt.Sprintf("created flag %s\nselected the %s SDK", m.flagKey, m.sdk.DisplayName)
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
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

0 comments on commit f076a59

Please sign in to comment.