Skip to content

Commit

Permalink
Merge pull request #309 from rusq/i273-auth
Browse files Browse the repository at this point in the history
Offer to authenticate if there are no workspaces
  • Loading branch information
rusq authored Jul 26, 2024
2 parents 03d4d73 + 88e2077 commit eb05e55
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 33 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
}
42 changes: 21 additions & 21 deletions cmd/slackdump/internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,6 @@ func argsWorkspace(args []string, defaultWsp string) string {
return ""
}

// Auth authenticates in the workspace wsp, and saves, or reuses the credentials
// in the dir.
func Auth(ctx context.Context, cacheDir string, wsp string, usePlaywright bool) (auth.Provider, error) {
m, err := cache.NewManager(cacheDir)
if err != nil {
return nil, err
}
if !m.Exists(wsp) {
return nil, fmt.Errorf("workspace does not exist: %q", cfg.Workspace)
}

prov, err := m.Auth(ctx, wsp, cache.AuthData{Token: cfg.SlackToken, Cookie: cfg.SlackCookie, UsePlaywright: usePlaywright})
if err != nil {
return nil, err
}
return prov, nil
}

// AuthCurrent authenticates in the current workspace, or overrideWsp if it's
// provided.
func AuthCurrent(ctx context.Context, cacheDir string, overrideWsp string, usePlaywright bool) (auth.Provider, error) {
Expand All @@ -105,7 +87,7 @@ func AuthCurrent(ctx context.Context, cacheDir string, overrideWsp string, usePl
}
trace.Logf(ctx, "AuthCurrent", "current workspace=%s", wsp)

prov, err := Auth(ctx, cacheDir, wsp, usePlaywright)
prov, err := authWsp(ctx, cacheDir, wsp, usePlaywright)
if err != nil {
return nil, err
}
Expand All @@ -120,12 +102,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 All @@ -138,3 +119,22 @@ func Current(cacheDir string, override string) (wsp string, err error) {
}
return wsp, nil
}

// authWsp authenticates in the workspace wsp, and saves, or reuses the
// credentials in the cacheDir. It returns ErrNotExists if the workspace
// doesn't exist in the cacheDir.
func authWsp(ctx context.Context, cacheDir string, wsp string, usePlaywright bool) (auth.Provider, error) {
m, err := cache.NewManager(cacheDir)
if err != nil {
return nil, err
}
if err := m.ExistsErr(wsp); err != nil {
return nil, err
}

prov, err := m.Auth(ctx, wsp, cache.AuthData{Token: cfg.SlackToken, Cookie: cfg.SlackCookie, UsePlaywright: usePlaywright})
if err != nil {
return nil, err
}
return prov, nil
}
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
15 changes: 12 additions & 3 deletions internal/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,20 @@ func (m *Manager) FileInfo(name string) (fs.FileInfo, error) {
// Exists returns true if the workspace with name "name" exists in the list of
// authenticated workspaces.
func (m *Manager) Exists(name string) bool {
existing, err := m.List()
return m.ExistsErr(name) == nil
}

// ExistsErr checks if the workspace exists, and returns any errors that may
// occur.
func (m *Manager) ExistsErr(name string) error {
ws, err := m.List()
if err != nil {
return false
return err
}
return slices.Contains(existing, name)
if !slices.Contains(ws, name) {
return newErrNoWorkspace(name)
}
return nil
}

// filename returns the filename for the workspace name.
Expand Down
40 changes: 40 additions & 0 deletions internal/cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Test_currentWsp(t *testing.T) {
}
}

// prepareDir creates a directory with test files.
func prepareDir(t *testing.T, dir string) {
for _, filename := range testFiles(dir) {
if err := os.WriteFile(filename, []byte("dummy"), 0600); err != nil {
Expand Down Expand Up @@ -112,3 +113,42 @@ func TestManager_listFiles(t *testing.T) {
})
}
}

func TestManager_ExistsErr(t *testing.T) {
t.Parallel()
t.Run("empty directory", func(t *testing.T) {
t.Parallel()

tempdir := t.TempDir()
m := &Manager{
dir: tempdir,
}
err := m.ExistsErr("foo")
assert.ErrorIs(t, err, ErrNoWorkspaces)
})
t.Run("workspace exists", func(t *testing.T) {
t.Parallel()

tempdir := t.TempDir()
prepareDir(t, tempdir)
m := &Manager{
dir: tempdir,
}
err := m.ExistsErr("foo")
assert.NoError(t, err)
})
t.Run("workspace does not exist", func(t *testing.T) {
t.Parallel()

tempdir := t.TempDir()
prepareDir(t, tempdir)
m := &Manager{
dir: tempdir,
}
err := m.ExistsErr("baz")
var e *ErrWorkspace
assert.ErrorAs(t, err, &e)
assert.Equal(t, e.Message, "no such workspace")
assert.Equal(t, e.Workspace, "baz")
})
}

0 comments on commit eb05e55

Please sign in to comment.