Skip to content

Commit

Permalink
added login
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Guduru committed Mar 14, 2024
1 parent 88765a2 commit 0310cd0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
116 changes: 116 additions & 0 deletions internal/setup/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package setup

// A simple example that shows how to retrieve a value from a Bubble Tea
// program after the Bubble Tea has exited.

import (
"fmt"
"io"
"strings"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)

type loginMethod struct {
Label string `json:"name"`
Key string `json:"key"`
}

func (p loginMethod) FilterValue() string { return "" }

type loginModel struct {
choice string
err error
list list.Model
}

func NewLogin() loginModel {
choices := []loginMethod{
{
Label: "Create a new account",
Key: "new-account",
},
{
Label: "OAuth",
Key: "oauth",
},
{
Label: "Personal access token",
Key: "access-token",
},
{
Label: "Service token",
Key: "service-token",
},
}
l := list.New(loginMethodsToItems(choices), loginDelegate{}, 30, 14)
l.Title = "Log Into LaunchDarkly"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)

return loginModel{
list: l,
}
}

func (m loginModel) Init() tea.Cmd {
return nil
}

func (m loginModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Enter):
i, ok := m.list.SelectedItem().(loginMethod)
if ok {
m.choice = i.Key
}
case key.Matches(msg, keys.Quit):
return m, tea.Quit
default:
m.list, cmd = m.list.Update(msg)
}
}

return m, cmd
}

func (m loginModel) View() string {
return "\n" + m.list.View()
}

type loginDelegate struct{}

func (d loginDelegate) Height() int { return 1 }
func (d loginDelegate) Spacing() int { return 0 }
func (d loginDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d loginDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(loginMethod)
if !ok {
return
}

str := i.Label

fn := choiceStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedChoiceItemStyle.Render("> " + strings.Join(s, " "))
}
}

fmt.Fprint(w, fn(str))
}

func loginMethodsToItems(loginMethods []loginMethod) []list.Item {
items := make([]list.Item, len(loginMethods))
for i, m := range loginMethods {
items[i] = list.Item(m)
}

return items
}
14 changes: 11 additions & 3 deletions internal/setup/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type sessionState int

// list of steps in the wizard
const (
autoCreateStep sessionState = iota
loginStep sessionState = iota
autoCreateStep
projectsStep
environmentsStep
flagsStep
Expand All @@ -36,14 +37,15 @@ func NewWizardModel() tea.Model {
// Since there isn't a model for the initial step, the currStep value will always be one ahead of the step in
// this slice. It may be convenient to add a model for the initial step to contain its own view logic and to
// prevent this off-by-one issue.
NewLogin(),
NewAutoCreate(),
NewProject(),
NewEnvironment(),
NewFlag(),
}

return WizardModel{
currStep: autoCreateStep,
currStep: 0,
steps: steps,
}
}
Expand All @@ -60,6 +62,12 @@ func (m WizardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keys.Enter):
switch m.currStep {
case loginStep:
model, _ := m.steps[loginStep].Update(msg)
_, ok := model.(loginModel)
if ok {
m.currStep += 1
}
case autoCreateStep:
model, _ := m.steps[autoCreateStep].Update(msg)
p, ok := model.(autoCreateModel)
Expand Down Expand Up @@ -115,7 +123,7 @@ func (m WizardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case key.Matches(msg, keys.Back):
// only go back if not on the first step
if m.currStep > autoCreateStep {
if m.currStep > 0 {
m.currStep -= 1
}
case key.Matches(msg, keys.Quit):
Expand Down

0 comments on commit 0310cd0

Please sign in to comment.