Skip to content

Commit

Permalink
lowercase workspace name
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 29, 2023
1 parent 138aff6 commit 7ac67dd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions auth/browser/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ var installFn = playwright.Install

// New create new browser based client.
func New(workspace string, opts ...Option) (*Client, error) {
if workspace == "" {
if strings.TrimSpace(workspace) == "" {
return nil, errors.New("workspace can't be empty")
}
cl := &Client{
workspace: workspace,
workspace: strings.ToLower(workspace),
pageClosed: make(chan bool, 1),
br: Bfirefox,
loginTimeout: float64(DefLoginTimeout.Milliseconds()),
Expand Down
3 changes: 2 additions & 1 deletion auth/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"strings"
"time"

"github.com/rusq/slackdump/v2/auth/browser"
Expand All @@ -23,7 +24,7 @@ func BrowserWithAuthFlow(flow BrowserAuthUI) Option {

func BrowserWithWorkspace(name string) Option {
return func(o *options) {
o.browserOpts.workspace = name
o.browserOpts.workspace = strings.ToLower(name)
}
}

Expand Down
7 changes: 4 additions & 3 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"
"strings"

"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
Expand Down Expand Up @@ -65,11 +66,11 @@ type manager interface {
// if it is. Otherwise, it checks the first (with index zero) argument in args,
// and if it set, returns it. Otherwise, it returns an empty string.
func argsWorkspace(args []string, defaultWsp string) string {
if defaultWsp != "" {
return defaultWsp
if strings.TrimSpace(defaultWsp) != "" {
return strings.ToLower(defaultWsp)
}
if len(args) > 0 && args[0] != "" {
return args[0]
return strings.ToLower(args[0])
}

return ""
Expand Down
47 changes: 47 additions & 0 deletions cmd/slackdump/internal/workspace/workspace_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
package workspace

import "testing"

func Test_argsWorkspace(t *testing.T) {
type args struct {
args []string
defaultWsp string
}
tests := []struct {
name string
args args
want string
}{
{
"empty",
args{[]string{}, ""},
"",
},
{
"default is set, no workspace in args",
args{[]string{}, "default"},
"default",
},
{
"default overrides args args",
args{[]string{"arg"}, "default"},
"default",
},
{
"returns must be lowercase",
args{[]string{"UPPERCASE"}, "DEFAULT"},
"default",
},
{
"returns must be lowercase",
args{[]string{"UPPERCASE"}, ""},
"uppercase",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := argsWorkspace(tt.args.args, tt.args.defaultWsp); got != tt.want {
t.Errorf("argsWorkspace() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 3 additions & 1 deletion internal/cache/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"runtime"
"runtime/trace"
"strings"

"github.com/rusq/encio"

Expand Down Expand Up @@ -67,6 +68,7 @@ func (c SlackCreds) AuthProvider(ctx context.Context, workspace string, opts ...
if err != nil {
return nil, err
}
workspace = strings.ToLower(workspace)

opts = append([]auth.Option{auth.BrowserWithWorkspace(workspace)}, opts...)

Expand Down Expand Up @@ -154,7 +156,7 @@ func initProvider(ctx context.Context, cacheDir string, filename string, workspa

// init the authentication provider
trace.Log(ctx, "info", "getting credentals from file or browser")
provider, err := creds.AuthProvider(ctx, workspace, opts...)
provider, err := creds.AuthProvider(ctx, strings.ToLower(workspace), opts...)
if err != nil {
return nil, fmt.Errorf("failed to initialise the auth provider: %w", err)
}
Expand Down

0 comments on commit 7ac67dd

Please sign in to comment.