Skip to content

Commit

Permalink
fix auth flow when no workspaces present
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jul 26, 2024
1 parent 03d4d73 commit cb61290
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
8 changes: 1 addition & 7 deletions cmd/slackdump/internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ import (
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
)

// CurrentProvider is a shortcut function to initialise the current auth
// provider.
func CurrentProvider(ctx context.Context) (auth.Provider, error) {
return workspace.AuthCurrent(ctx, cfg.CacheDir(), cfg.Workspace, cfg.LegacyBrowser)
}

// CurrentProviderCtx returns the context with the current provider.
func CurrentProviderCtx(ctx context.Context) (context.Context, error) {
prov, err := workspace.AuthCurrent(ctx, cfg.CacheDir(), cfg.Workspace, cfg.LegacyBrowser)
if err != nil {
return nil, err
return ctx, err
}
return auth.WithContext(ctx, prov), nil
}
12 changes: 8 additions & 4 deletions cmd/slackdump/internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"runtime/trace"
"slices"
"strings"

"github.com/rusq/slackdump/v3/auth"
Expand Down Expand Up @@ -85,8 +86,12 @@ func Auth(ctx context.Context, cacheDir string, wsp string, usePlaywright bool)
if err != nil {
return nil, err
}
if !m.Exists(wsp) {
return nil, fmt.Errorf("workspace does not exist: %q", cfg.Workspace)
all, err := m.List()
if err != nil {
return nil, err
}
if !slices.Contains(all, wsp) {
return nil, fmt.Errorf("%w: %q", ErrNotExists, cfg.Workspace)
}

prov, err := m.Auth(ctx, wsp, cache.AuthData{Token: cfg.SlackToken, Cookie: cfg.SlackCookie, UsePlaywright: usePlaywright})
Expand Down Expand Up @@ -120,12 +125,11 @@ func Current(cacheDir string, override string) (wsp string, err error) {
if err != nil {
return "", err
}

if override != "" {
if m.Exists(override) {
return override, nil
}
return "", fmt.Errorf("workspace does not exist: %q", override)
return "", fmt.Errorf("%w: %q", ErrNotExists, override)
}

wsp, err = m.Current()
Expand Down
19 changes: 17 additions & 2 deletions cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -33,6 +34,7 @@ import (
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/view"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/wizard"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
"github.com/rusq/slackdump/v3/internal/cache"
"github.com/rusq/slackdump/v3/logger"
)

Expand Down Expand Up @@ -186,8 +188,21 @@ func invoke(cmd *base.Command, args []string) error {
ctx, err = bootstrap.CurrentProviderCtx(ctx)
if err != nil {
trace.Logf(ctx, "invoke", "auth error: %s", err)
base.SetExitStatus(base.SAuthError)
return fmt.Errorf("auth error: %w", err)
if errors.Is(err, cache.ErrNoWorkspaces) {
// ask to create a new workspace
if err := workspace.CmdWspNew.Run(ctx, cmd, args); err != nil {
base.SetExitStatus(base.SAuthError)
return fmt.Errorf("auth error: %w", err)
}
ctx, err = bootstrap.CurrentProviderCtx(ctx)
if err != nil {
base.SetExitStatus(base.SAuthError)
return fmt.Errorf("auth error: %w", err)
}
} else {
base.SetExitStatus(base.SAuthError)
return fmt.Errorf("auth error: %w", err)
}
}
}
trace.Log(ctx, "command", fmt.Sprint("Running ", cmd.Name(), " command"))
Expand Down

0 comments on commit cb61290

Please sign in to comment.