Skip to content

Commit

Permalink
workspace->new fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 15, 2024
1 parent 381ef85 commit b78f4a3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 27 deletions.
44 changes: 29 additions & 15 deletions auth/auth_ui/huh.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package auth_ui

import (
"context"
"errors"
"fmt"
"io"
"regexp"
"strconv"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/huh"
"github.com/rusq/slackauth"
)
Expand All @@ -33,7 +35,7 @@ func (h *Huh) RequestWorkspace(w io.Writer) (string, error) {

func (*Huh) Stop() {}

func (*Huh) RequestCreds(w io.Writer, workspace string) (email string, passwd string, err error) {
func (*Huh) RequestCreds(ctx context.Context, w io.Writer, workspace string) (email string, passwd string, err error) {
f := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Expand All @@ -46,8 +48,8 @@ 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()
).WithTheme(Theme).WithKeyMap(keymap)
err = f.RunWithContext(ctx)
return
}

Expand Down Expand Up @@ -85,15 +87,13 @@ type LoginOpts struct {
BrowserPath string
}

func valWorkspace(s string) error {
if err := valRequired(s); err != nil {
return err
}
_, err := Sanitize(s)
return err
var keymap = huh.NewDefaultKeyMap()

func init() {
keymap.Quit = key.NewBinding(key.WithKeys("esc", "ctrl+c"), key.WithHelp("esc", "Quit"))
}

func (*Huh) RequestLoginType(w io.Writer, workspace string) (LoginOpts, error) {
func (*Huh) RequestLoginType(ctx context.Context, w io.Writer, workspace string) (LoginOpts, error) {
var ret = LoginOpts{
Workspace: workspace,
Type: LInteractive,
Expand All @@ -120,7 +120,11 @@ func (*Huh) RequestLoginType(w io.Writer, workspace string) (LoginOpts, error) {

fields = append(fields, huh.NewSelect[LoginType]().
TitleFunc(func() string {
return fmt.Sprintf("Select login type for [%s]", ret.Workspace)
wsp, err := Sanitize(ret.Workspace)
if err != nil {
return "Select login type"
}
return fmt.Sprintf("Select login type for [%s]", wsp)
}, &ret.Workspace).
Options(opts...).
Value(&ret.Type).
Expand All @@ -139,21 +143,31 @@ func (*Huh) RequestLoginType(w io.Writer, workspace string) (LoginOpts, error) {
return ""
}
}, &ret.Type))
if err := huh.NewForm(huh.NewGroup(fields...)).WithTheme(Theme).Run(); err != nil {

form := huh.NewForm(huh.NewGroup(fields...)).WithTheme(Theme).WithKeyMap(keymap)

if err := form.RunWithContext(ctx); err != nil {
return ret, err
}
var err error
ret.Workspace, err = Sanitize(ret.Workspace)
if err != nil {
return ret, err
}

if ret.Type == LUserBrowser {
path, err := chooseBrowser()
path, err := chooseBrowser(ctx)
if err != nil {
return ret, err
}
ret.BrowserPath = path
return ret, err
}

return ret, nil
}

func chooseBrowser() (string, error) {
func chooseBrowser(ctx context.Context) (string, error) {
browsers, err := slackauth.ListBrowsers()
if err != nil {
return "", err
Expand All @@ -172,7 +186,7 @@ func chooseBrowser() (string, error) {
DescriptionFunc(func() string {
return browsers[selection].Path
}, &selection),
)).WithTheme(Theme).Run()
)).WithTheme(Theme).WithKeyMap(keymap).RunWithContext(ctx)
if err != nil {
return "", err
}
Expand Down
8 changes: 8 additions & 0 deletions auth/auth_ui/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ func valSepEaster() func(v LoginType) error {
return nil
}
}

func valWorkspace(s string) error {
if err := valRequired(s); err != nil {
return err
}
_, err := Sanitize(s)
return err
}
8 changes: 4 additions & 4 deletions auth/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ type browserAuthUIExt interface {
// RequestLoginType should request the login type from the user and return
// one of the [auth_ui.LoginType] constants. The implementation should
// provide a way to cancel the login flow, returning [auth_ui.LoginCancel].
RequestLoginType(w io.Writer, workspace string) (auth_ui.LoginOpts, error)
RequestLoginType(ctx context.Context, w io.Writer, workspace string) (auth_ui.LoginOpts, error)
// RequestCreds should request the user's email and password and return
// them.
RequestCreds(w io.Writer, workspace string) (email string, passwd string, err error)
RequestCreds(ctx context.Context, w io.Writer, workspace string) (email string, passwd string, err error)
// ConfirmationCode should request the confirmation code from the user and
// return it.
ConfirmationCode(email string) (code int, err error)
Expand Down Expand Up @@ -95,7 +95,7 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
r.opts.workspace = wsp
}

resp, err := r.opts.ui.RequestLoginType(os.Stdout, r.opts.workspace)
resp, err := r.opts.ui.RequestLoginType(ctx, os.Stdout, r.opts.workspace)
if err != nil {
return r, err
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
}

func headlessFlow(ctx context.Context, cl *slackauth.Client, workspace string, ui browserAuthUIExt) (sp simpleProvider, err error) {
username, password, err := ui.RequestCreds(os.Stdout, workspace)
username, password, err := ui.RequestCreds(ctx, os.Stdout, workspace)
if err != nil {
return sp, err
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/slackdump/internal/ui/bubbles/menu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ func (m *Model) View() string {
return m.view()
}

func (m *Model) Select(id string) {
if id == m.items[m.cursor].ID {
return
}
for i, item := range m.items {
if item.ID == id && !item.Separator {
m.cursor = i
break
}
}
}

func capfirst(s string) string {
if s == "" {
return ""
Expand Down
19 changes: 14 additions & 5 deletions cmd/slackdump/internal/workspace/workspaceui/ezlogin3000.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func ezLogin3000(ctx context.Context, mgr manager) error {
)
form := huh.NewForm(huh.NewGroup(
huh.NewConfirm().
Title("Use legacy EZ-Login?").
Description("Do you want to use the legacy login?").
Title("Do you want to use the legacy login?").
Description("Choose 'Yes' if you had problems in the past with the current EZ-Login.").
Value(&legacy),
)).WithTheme(ui.HuhTheme()).WithKeyMap(ui.DefaultHuhKeymap)
if err := form.RunWithContext(ctx); err != nil {
Expand All @@ -28,11 +28,20 @@ func ezLogin3000(ctx context.Context, mgr manager) error {
}
return err
}

var err error
if legacy {
return playwrightLogin(ctx, mgr)
err = playwrightLogin(ctx, mgr)
} else {
err = rodLogin(ctx, mgr)
}
return rodLogin(ctx, mgr)

if err != nil {
if errors.Is(err, auth.ErrCancelled) {
return nil
}
return err
}
return nil
}

func playwrightLogin(ctx context.Context, mgr manager) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func fileWithSecrets(ctx context.Context, mgr manager) error {
ShowPermissions(true).
Value(&filename).
Validate(validateSecrets),
)).WithTheme(ui.HuhTheme()).WithHeight(10)
)).WithTheme(ui.HuhTheme()).WithHeight(10).WithKeyMap(ui.DefaultHuhKeymap)
if err := form.RunWithContext(ctx); err != nil {
if errors.Is(err, huh.ErrUserAborted) {
return nil
Expand Down
7 changes: 5 additions & 2 deletions cmd/slackdump/internal/workspace/workspaceui/workspaceui.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func WorkspaceNew(ctx context.Context, _ *base.Command, _ []string) error {
{
ID: actLogin,
Name: "Login in Browser",
Help: "Login to Slack in your browser",
Help: "Opens the browser and lets you login in a familiar way.",
},
{
ID: actToken,
Name: "Token/Cookie",
Help: "Enter token and cookie that you grabbed from the browser",
Help: "Enter token and cookie that you grabbed from the browser.",
},
{
ID: actTokenFile,
Expand Down Expand Up @@ -66,12 +66,15 @@ func WorkspaceNew(ctx context.Context, _ *base.Command, _ []string) error {
actSecrets: fileWithSecrets,
}

var lastID string = actLogin
LOOP:
for {
m := menu.New("New Workspace", items, true)
m.Select(lastID)
if _, err := tea.NewProgram(&wizModel{m: m}, tea.WithContext(ctx)).Run(); err != nil {
return err
}
lastID = m.Selected.ID
if m.Cancelled {
break LOOP
}
Expand Down

0 comments on commit b78f4a3

Please sign in to comment.