From 7ac67dd0bfc2600e66ca808972c25d092ff84aae Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Wed, 29 Nov 2023 19:10:21 +1000 Subject: [PATCH] lowercase workspace name --- auth/browser/client.go | 4 +- auth/option.go | 3 +- cmd/slackdump/internal/workspace/workspace.go | 7 +-- .../internal/workspace/workspace_test.go | 47 +++++++++++++++++++ internal/cache/auth.go | 4 +- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/auth/browser/client.go b/auth/browser/client.go index 43242ce8..bc14d17a 100644 --- a/auth/browser/client.go +++ b/auth/browser/client.go @@ -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()), diff --git a/auth/option.go b/auth/option.go index 08b9c3ae..ab076e8e 100644 --- a/auth/option.go +++ b/auth/option.go @@ -1,6 +1,7 @@ package auth import ( + "strings" "time" "github.com/rusq/slackdump/v2/auth/browser" @@ -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) } } diff --git a/cmd/slackdump/internal/workspace/workspace.go b/cmd/slackdump/internal/workspace/workspace.go index 69f0befe..feb7243e 100644 --- a/cmd/slackdump/internal/workspace/workspace.go +++ b/cmd/slackdump/internal/workspace/workspace.go @@ -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" @@ -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 "" diff --git a/cmd/slackdump/internal/workspace/workspace_test.go b/cmd/slackdump/internal/workspace/workspace_test.go index d9031cdd..1af9a694 100644 --- a/cmd/slackdump/internal/workspace/workspace_test.go +++ b/cmd/slackdump/internal/workspace/workspace_test.go @@ -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) + } + }) + } +} diff --git a/internal/cache/auth.go b/internal/cache/auth.go index 9928fbf8..f04ed5e2 100644 --- a/internal/cache/auth.go +++ b/internal/cache/auth.go @@ -9,6 +9,7 @@ import ( "path/filepath" "runtime" "runtime/trace" + "strings" "github.com/rusq/encio" @@ -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...) @@ -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) }