Skip to content

Commit

Permalink
new workspace wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 12, 2024
1 parent 0de0cdf commit 589a53a
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 28 deletions.
12 changes: 7 additions & 5 deletions auth/auth_ui/huh.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
// Huh is the Auth UI that uses the huh library to provide a terminal UI.
type Huh struct{}

var Theme = huh.ThemeBase16()

func (h *Huh) RequestWorkspace(w io.Writer) (string, error) {
var workspace string
err := huh.NewForm(huh.NewGroup(
Expand All @@ -22,7 +24,7 @@ func (h *Huh) RequestWorkspace(w io.Writer) (string, error) {
Value(&workspace).
Validate(valWorkspace).
Description("The workspace name is the part of the URL that comes before `.slack.com' in\nhttps://<workspace>.slack.com/. Both workspace name or URL are acceptable."),
)).Run()
)).WithTheme(Theme).Run()
if err != nil {
return "", err
}
Expand All @@ -44,7 +46,7 @@ func (*Huh) RequestCreds(w io.Writer, workspace string) (email string, passwd st
Placeholder("your slack password").
Validate(valRequired).EchoMode(huh.EchoModePassword),
),
)
).WithTheme(Theme)
err = f.Run()
return
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func (*Huh) RequestLoginType(w io.Writer, workspace string) (LoginOpts, error) {
return ""
}
}, &ret.Type))
if err := huh.NewForm(huh.NewGroup(fields...)).Run(); err != nil {
if err := huh.NewForm(huh.NewGroup(fields...)).WithTheme(Theme).Run(); err != nil {
return ret, err
}
if ret.Type == LUserBrowser {
Expand Down Expand Up @@ -170,7 +172,7 @@ func chooseBrowser() (string, error) {
DescriptionFunc(func() string {
return browsers[selection].Path
}, &selection),
)).Run()
)).WithTheme(Theme).Run()
if err != nil {
return "", err
}
Expand All @@ -189,7 +191,7 @@ func (*Huh) ConfirmationCode(email string) (int, error) {
Description("Slack did not recognise the browser, and sent a confirmation code. Please enter the confirmation code below.").
Value(&strCode).
Validate(valSixDigits),
))
)).WithTheme(Theme)
if err := q.Run(); err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/ui/filepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewFilePicker(prompt string, homedir string, allowedExt ...string) FileSyst
fp := filepicker.New()
fp.AllowedTypes = allowedExt
fp.CurrentDirectory = homedir
fp.Styles.Cursor = HuhTheme.Focused.SelectedOption
fp.Styles.Cursor = HuhTheme().Focused.SelectedOption

return FileSystemModel{
filepicker: fp,
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/ui/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func FileSelector(msg, descr string, opt ...Option) (string, error) {
var resp struct {
Filename string
}
q := huh.NewForm(huh.NewGroup(fieldFileInput(&resp.Filename, msg, descr, *opts))).WithTheme(HuhTheme)
q := huh.NewForm(huh.NewGroup(fieldFileInput(&resp.Filename, msg, descr, *opts))).WithTheme(HuhTheme())

for {
if err := q.Run(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Input(msg, help string, validateFn func(s string) error) (string, error) {
Title(msg).
Description(help).
Validate(validateFn).
Value(&resp))).WithTheme(HuhTheme).
Value(&resp))).WithTheme(HuhTheme()).
Run(); err != nil {
return "", err
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/slackdump/internal/ui/keymap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ui

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/huh"
)

var DefaultHuhKeymap = huh.NewDefaultKeyMap()

func init() {
// redefinition of some of the default keys.
DefaultHuhKeymap.Quit = key.NewBinding(key.WithKeys("ctrl+c", "esc"), key.WithHelp("esc", "quit"))
}
5 changes: 3 additions & 2 deletions cmd/slackdump/internal/ui/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/charmbracelet/lipgloss"
)

var HuhTheme = ThemeBase16Ext() // Theme is the default Wizard theme.
// HuhTheme is the default Wizard theme.
var HuhTheme = ThemeBase16Ext

type Theme struct {
Focused ControlStyle
Expand Down Expand Up @@ -127,7 +128,7 @@ func ThemeBase16Ext() *huh.Theme {
t.Focused.Title = t.Focused.Title.Foreground(cyan)
t.Focused.NoteTitle = t.Focused.NoteTitle.Foreground(cyan)
t.Focused.Directory = t.Focused.Directory.Foreground(cyan)
t.Focused.Description = t.Focused.Description.Foreground(gray)
t.Focused.Description = t.Focused.Description.Foreground(white)
t.Focused.ErrorIndicator = t.Focused.ErrorIndicator.Foreground(ltred)
t.Focused.ErrorMessage = t.Focused.ErrorMessage.Foreground(ltred)
t.Focused.SelectSelector = t.Focused.SelectSelector.Foreground(yellow)
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/ui/updaters/picklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewPicklist[T comparable](v *T, s *huh.Select[T]) *Model[T] {
m := &Model[T]{
s: s.Value(v).
Description("Select an option").
WithTheme(ui.HuhTheme).
WithTheme(ui.HuhTheme()).
WithKeyMap(huh.NewDefaultKeyMap()),
help: help.New(),

Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/wizard/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newModel(m *menu) model {
Description("Slack workspace: " + bootstrap.CurrentWsp()).
Options(options...),
),
).WithTheme(ui.HuhTheme),
).WithTheme(ui.HuhTheme()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/workspace/wiz_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ func (m selectModel) View() string {
if m.finished {
return "" // don't render the table if we've selected a workspace
}
return m.style.FocusedBorder.Render((m.table.View()) + "\n\n" + ui.HuhTheme.Help.Ellipsis.Render("Select the workspace with arrow keys, press [Enter] to confirm, [Esc] to cancel."))
return m.style.FocusedBorder.Render((m.table.View()) + "\n\n" + ui.HuhTheme().Help.Ellipsis.Render("Select the workspace with arrow keys, press [Enter] to confirm, [Esc] to cancel."))
}
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/workspace/workspaceui/dialogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func askRetry(ctx context.Context, name string, err error) (retry bool) {
huh.NewConfirm().Title("Error Creating Workspace").
Description(msg).
Value(&retry).Affirmative("Retry").Negative("Cancel"),
)).WithTheme(ui.HuhTheme).RunWithContext(ctx); err != nil {
)).WithTheme(ui.HuhTheme()).RunWithContext(ctx); err != nil {
return false
}
return retry
Expand All @@ -30,5 +30,5 @@ func success(ctx context.Context, workspace string) error {
Description(fmt.Sprintf("Workspace %q was added and selected.\n\n", workspace)).
Next(true).
NextLabel("Exit"),
)).WithTheme(ui.HuhTheme).RunWithContext(ctx)
)).WithTheme(ui.HuhTheme()).RunWithContext(ctx)
}
76 changes: 76 additions & 0 deletions cmd/slackdump/internal/workspace/workspaceui/ezlogin3000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package workspaceui

import (
"context"
"errors"

"github.com/rusq/slackdump/v3/auth"

"github.com/rusq/slackdump/v3/auth/browser"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"

"github.com/charmbracelet/huh"
)

func ezLogin3000(ctx context.Context, mgr manager) error {
var (
legacy bool
)
form := huh.NewForm(huh.NewGroup(
huh.NewConfirm().
Title("Use legacy EZ-Login?").
Description("Do you want to use the legacy login?").
Value(&legacy),
)).WithTheme(ui.HuhTheme()).WithKeyMap(ui.DefaultHuhKeymap)
if err := form.RunWithContext(ctx); err != nil {
if errors.Is(err, huh.ErrUserAborted) {
return nil
}
return err
}
if legacy {
return playwrightLogin(ctx, mgr)
}
return rodLogin(ctx, mgr)

}

func playwrightLogin(ctx context.Context, mgr manager) error {
var brws = browser.Bchromium
formBrowser := huh.NewForm(huh.NewGroup(
huh.NewSelect[browser.Browser]().
Options(
huh.NewOption("Chromium", browser.Bchromium),
huh.NewOption("Firefox", browser.Bfirefox),
).
Value(&brws),
)).WithTheme(ui.HuhTheme()).WithKeyMap(ui.DefaultHuhKeymap)
if err := formBrowser.RunWithContext(ctx); err != nil {
if errors.Is(err, huh.ErrUserAborted) {
return nil
}
return err
}
prov, err := auth.NewBrowserAuth(ctx, auth.BrowserWithBrowser(brws))
if err != nil {
return err
}

name, err := createAndSelect(ctx, mgr, prov)
if err != nil {
return err
}
return success(ctx, name)
}

func rodLogin(ctx context.Context, mgr manager) error {
prov, err := auth.NewRODAuth(ctx)
if err != nil {
return err
}
name, err := createAndSelect(ctx, mgr, prov)
if err != nil {
return err
}
return success(ctx, name)
}
9 changes: 5 additions & 4 deletions cmd/slackdump/internal/workspace/workspaceui/tokencookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func prgTokenCookie(ctx context.Context, m manager) error {
workspace string
confirmed bool
)

for !confirmed {
f := huh.NewForm(huh.NewGroup(
huh.NewInput().Title("Token").
Expand All @@ -30,11 +31,11 @@ func prgTokenCookie(ctx context.Context, m manager) error {
Placeholder("xoxd-...").
Value(&cookie),
huh.NewConfirm().Title("Confirm creation of workspace?").
Description("Once confirmed this will create a new workspace with the provided token and cookie").
Description("Once confirmed this will check the credentials for validity, detect the workspace \nand create a new workspace with the provided token and cookie").
Value(&confirmed).
Validate(makeValidator(ctx, &token, &cookie, auth.NewValueAuth)),
)).WithTheme(ui.HuhTheme)
if err := f.Run(); err != nil {
)).WithTheme(ui.HuhTheme()).WithKeyMap(ui.DefaultHuhKeymap)
if err := f.RunWithContext(ctx); err != nil {
return err
}
if !confirmed {
Expand Down Expand Up @@ -106,7 +107,7 @@ func prgTokenCookieFile(ctx context.Context, m manager) error {
Description("Once confirmed this will create a new workspace with the provided token and cookie").
Value(&confirmed).
Validate(makeValidator(ctx, &token, &cookiefile, auth.NewCookieFileAuth)),
)).WithTheme(ui.HuhTheme)
)).WithTheme(ui.HuhTheme()).WithKeyMap(ui.DefaultHuhKeymap)
if err := f.Run(); err != nil {
return err
}
Expand Down
22 changes: 14 additions & 8 deletions cmd/slackdump/internal/workspace/workspaceui/workspaceui.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ func WorkspaceNew(ctx context.Context, _ *base.Command, _ []string) error {
},
}

// new workspace methods
var methods = map[string]func(context.Context, manager) error{
actLogin: ezLogin3000,
actToken: prgTokenCookie,
actTokenFile: prgTokenCookieFile,
actSecrets: fileWithSecrets,
}

LOOP:
for {
m := menu.New("New Workspace", items, true)
Expand All @@ -67,16 +75,14 @@ LOOP:
if m.Cancelled {
break LOOP
}
var err error
switch m.Selected.ID {
case actToken:
err = prgTokenCookie(ctx, mgr)
case actTokenFile:
err = prgTokenCookieFile(ctx, mgr)
case actExit:
if m.Selected.ID == actExit {
break LOOP
}
if err != nil {
fn, ok := methods[m.Selected.ID]
if !ok {
return errors.New("internal error: unhandled login option")
}
if err := fn(ctx, mgr); err != nil {
if errors.Is(err, huh.ErrUserAborted) {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func whatDo() (choice, error) {
huh.NewOption(string(choiceHelp), choiceHelp),
huh.NewOption(string(choiceWizard), choiceWizard),
huh.NewOption(string(choiceExit), choiceExit),
).Value(&ans))).WithTheme(ui.HuhTheme).Run()
).Value(&ans))).WithTheme(ui.HuhTheme()).Run()

return ans, err
}
Expand Down

0 comments on commit 589a53a

Please sign in to comment.