From 3d48b313a30271df6024563046d1d23267411283 Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Thu, 28 Mar 2024 11:29:16 -0700 Subject: [PATCH 1/2] Show error along with message and quit if applicable --- internal/quickstart/container.go | 39 +++++++++++++++++++++--------- internal/quickstart/create_flag.go | 17 ++++++++++++- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/internal/quickstart/container.go b/internal/quickstart/container.go index 865dfb32..0b99b056 100644 --- a/internal/quickstart/container.go +++ b/internal/quickstart/container.go @@ -24,6 +24,7 @@ type ContainerModel struct { err error flagKey string flagsClient flags.Client + quitMsg string quitting bool steps []tea.Model } @@ -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) 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 } @@ -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 { diff --git a/internal/quickstart/create_flag.go b/internal/quickstart/create_flag.go index 1b0ece0d..16a7a3df 100644 --- a/internal/quickstart/create_flag.go +++ b/internal/quickstart/create_flag.go @@ -2,6 +2,7 @@ package quickstart import ( "context" + "encoding/json" "fmt" "github.com/charmbracelet/bubbles/key" @@ -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 } @@ -69,6 +72,18 @@ 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) + if e.Code == "unauthorized" || e.Code == "forbidden" { + m.quitting = true + m.quitMsg = "Try another api-key or contact your administrator." + + return m, tea.Quit + } return m, nil } From c0baceea34ac544baab245c6c98c55c09e392405 Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Thu, 28 Mar 2024 12:44:12 -0700 Subject: [PATCH 2/2] Split out messages based on response --- internal/quickstart/create_flag.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/quickstart/create_flag.go b/internal/quickstart/create_flag.go index 16a7a3df..e6444cf5 100644 --- a/internal/quickstart/create_flag.go +++ b/internal/quickstart/create_flag.go @@ -78,9 +78,15 @@ func (m createFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { Message string `json:"message"` } _ = json.Unmarshal([]byte(m.err.Error()), &e) - if e.Code == "unauthorized" || e.Code == "forbidden" { + switch { + case e.Code == "unauthorized": m.quitting = true - m.quitMsg = "Try another api-key or contact your administrator." + 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 }