Skip to content

Commit

Permalink
create CreateAndSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 15, 2024
1 parent 718b58b commit 148daaa
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 236 deletions.
32 changes: 0 additions & 32 deletions auth/auth_ui/auth_ui.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package auth_ui

import (
"errors"
"fmt"
"net/url"
"strings"
)

// LoginType is the login type, that is used to choose the authentication flow,
// for example login headlessly or interactively.
type LoginType int8
Expand All @@ -21,28 +14,3 @@ const (
// LCancel should be returned if the user cancels the login intent.
LCancel
)

var ErrInvalidDomain = errors.New("invalid domain")

// Sanitize takes a workspace name or URL and returns the workspace name.
func Sanitize(workspace string) (string, error) {
if !strings.Contains(workspace, ".slack.com") && !strings.Contains(workspace, ".") {
return workspace, nil
}
if strings.HasPrefix(workspace, "https://") {
uri, err := url.Parse(workspace)
if err != nil {
return "", err
}
workspace = uri.Host
}
// parse
name, domain, found := strings.Cut(workspace, ".")
if !found {
return "", errors.New("workspace name is empty")
}
if strings.TrimRight(domain, "/") != "slack.com" {
return "", fmt.Errorf("%s: %w", domain, ErrInvalidDomain)
}
return name, nil
}
34 changes: 0 additions & 34 deletions auth/auth_ui/auth_ui_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion auth/auth_ui/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/rusq/slackdump/v3/internal/structures"
"golang.org/x/term"
)

Expand All @@ -30,7 +31,7 @@ func (cl *CLI) RequestWorkspace(w io.Writer) (string, error) {
if err != nil {
return "", err
}
return Sanitize(workspace)
return structures.ExtractWorkspace(workspace)
}

func (*CLI) RequestCreds(w io.Writer, workspace string) (email string, passwd string, err error) {
Expand Down
5 changes: 3 additions & 2 deletions auth/auth_ui/huh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/huh"
"github.com/rusq/slackauth"
"github.com/rusq/slackdump/v3/internal/structures"
)

// Huh is the Auth UI that uses the huh library to provide a terminal UI.
Expand Down Expand Up @@ -120,7 +121,7 @@ func (*Huh) RequestLoginType(ctx context.Context, w io.Writer, workspace string)

fields = append(fields, huh.NewSelect[LoginType]().
TitleFunc(func() string {
wsp, err := Sanitize(ret.Workspace)
wsp, err := structures.ExtractWorkspace(ret.Workspace)
if err != nil {
return "Select login type"
}
Expand Down Expand Up @@ -150,7 +151,7 @@ func (*Huh) RequestLoginType(ctx context.Context, w io.Writer, workspace string)
return ret, err
}
var err error
ret.Workspace, err = Sanitize(ret.Workspace)
ret.Workspace, err = structures.ExtractWorkspace(ret.Workspace)
if err != nil {
return ret, err
}
Expand Down
4 changes: 3 additions & 1 deletion auth/auth_ui/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package auth_ui
import (
"errors"
"regexp"

"github.com/rusq/slackdump/v3/internal/structures"
)

var (
Expand Down Expand Up @@ -105,6 +107,6 @@ func valWorkspace(s string) error {
if err := valRequired(s); err != nil {
return err
}
_, err := Sanitize(s)
_, err := structures.ExtractWorkspace(s)
return err
}
3 changes: 2 additions & 1 deletion auth/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/rusq/slackdump/v3/auth/auth_ui"
"github.com/rusq/slackdump/v3/auth/browser"
"github.com/rusq/slackdump/v3/internal/structures"
)

var _ Provider = BrowserAuth{}
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewBrowserAuth(ctx context.Context, opts ...Option) (BrowserAuth, error) {
}
defer br.opts.flow.Stop()
}
if wsp, err := auth_ui.Sanitize(br.opts.workspace); err != nil {
if wsp, err := structures.ExtractWorkspace(br.opts.workspace); err != nil {
return br, err
} else {
br.opts.workspace = wsp
Expand Down
3 changes: 2 additions & 1 deletion auth/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rusq/slackauth"

"github.com/rusq/slackdump/v3/auth/auth_ui"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/logger"
)

Expand Down Expand Up @@ -89,7 +90,7 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
for _, opt := range opts {
opt(&r.opts)
}
if wsp, err := auth_ui.Sanitize(r.opts.workspace); err != nil {
if wsp, err := structures.ExtractWorkspace(r.opts.workspace); err != nil {
return r, err
} else {
r.opts.workspace = wsp
Expand Down
39 changes: 0 additions & 39 deletions cmd/slackdump/internal/workspace/workspaceui/api.go

This file was deleted.

117 changes: 0 additions & 117 deletions cmd/slackdump/internal/workspace/workspaceui/api_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/slackdump/internal/workspace/workspaceui/ezlogin3000.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func playwrightLogin(ctx context.Context, mgr manager) error {
return err
}

name, err := createAndSelect(ctx, mgr, prov)
name, err := mgr.CreateAndSelect(ctx, prov)
if err != nil {
return err
}
Expand All @@ -77,7 +77,7 @@ func rodLogin(ctx context.Context, mgr manager) error {
if err != nil {
return err
}
name, err := createAndSelect(ctx, mgr, prov)
name, err := mgr.CreateAndSelect(ctx, prov)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/workspace/workspaceui/filesecrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func fileWithSecrets(ctx context.Context, mgr manager) error {
if err != nil {
return err
}
wsp, err := createAndSelect(ctx, mgr, prov)
name, err := mgr.CreateAndSelect(ctx, prov)
if err != nil {
return err
}

return success(ctx, wsp)
return success(ctx, name)
}

func validateSecrets(filename string) error {
Expand Down
8 changes: 4 additions & 4 deletions cmd/slackdump/internal/workspace/workspaceui/tokencookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const sampleToken = "xoxc-610187951300-604451271234-3473161557912-4c426dd426a45208707725b710302b32dda0ab002b80ccd8c4c8ac9971a11558"

func prgTokenCookie(ctx context.Context, m manager) error {
func prgTokenCookie(ctx context.Context, mgr manager) error {
var (
token string
cookie string
Expand Down Expand Up @@ -46,7 +46,7 @@ func prgTokenCookie(ctx context.Context, m manager) error {
if err != nil {
return err
}
name, err := createAndSelect(ctx, m, prov)
name, err := mgr.CreateAndSelect(ctx, prov)
if err != nil {
confirmed = false
retry := askRetry(ctx, name, err)
Expand Down Expand Up @@ -83,7 +83,7 @@ func makeValidator[P auth.Provider](ctx context.Context, token *string, val *str
}
}

func prgTokenCookieFile(ctx context.Context, m manager) error {
func prgTokenCookieFile(ctx context.Context, mgr manager) error {
var (
token string
cookiefile string
Expand Down Expand Up @@ -116,7 +116,7 @@ func prgTokenCookieFile(ctx context.Context, m manager) error {
if err != nil {
return err
}
name, err := createAndSelect(ctx, m, prov)
name, err := mgr.CreateAndSelect(ctx, prov)
if err != nil {
confirmed = false
retry := askRetry(ctx, name, err)
Expand Down
Loading

0 comments on commit 148daaa

Please sign in to comment.