Skip to content

Commit

Permalink
Merge pull request #362 from rusq/fix-wsp-ent
Browse files Browse the repository at this point in the history
Fix new workspace creation wizard for enterprise instances + list chan/users fixes
  • Loading branch information
rusq authored Nov 20, 2024
2 parents 6104035 + 866e048 commit 28971d8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
25 changes: 20 additions & 5 deletions cmd/slackdump/internal/diag/eztest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/playwright-community/playwright-go"

"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
Expand All @@ -34,11 +35,12 @@ be printed and the test will be terminated.
}

type ezResult struct {
Engine string `json:"engine,omitempty"`
HasToken bool `json:"has_token,omitempty"`
HasCookies bool `json:"has_cookies,omitempty"`
Err *string `json:"error,omitempty"`
Credentials *Credentials `json:"credentials,omitempty"`
Engine string `json:"engine,omitempty"`
HasToken bool `json:"has_token,omitempty"`
HasCookies bool `json:"has_cookies,omitempty"`
Err *string `json:"error,omitempty"`
Credentials *Credentials `json:"credentials,omitempty"`
Response *slack.AuthTestResponse `json:"response,omitempty"`
}

type Credentials struct {
Expand Down Expand Up @@ -125,6 +127,12 @@ func tryPlaywrightAuth(ctx context.Context, wsp string, populateCreds bool) ezRe
Token: prov.SlackToken(),
Cookies: prov.Cookies(),
}
resp, err := prov.Test(ctx)
if err != nil {
ret.Err = ptr(err.Error())
return ret
}
ret.Response = resp
}
return ret
}
Expand All @@ -138,13 +146,20 @@ func tryRodAuth(ctx context.Context, wsp string, populateCreds bool) ezResult {
ret.Err = ptr(err.Error())
return ret
}

ret.HasCookies = len(prov.Cookies()) > 0
ret.HasToken = len(prov.SlackToken()) > 0
if populateCreds {
ret.Credentials = &Credentials{
Token: prov.SlackToken(),
Cookies: prov.Cookies(),
}
resp, err := prov.Test(ctx)
if err != nil {
ret.Err = ptr(err.Error())
return ret
}
ret.Response = resp
}
return ret
}
25 changes: 15 additions & 10 deletions cmd/slackdump/internal/list/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,40 @@ func wizUsers(ctx context.Context, _ *base.Command, _ []string) error {
return err
}

filename = makeFilename("users", sess.Info().TeamID, ".json")
filename = ""
w := dumpui.Wizard{
Title: "List Users",
Name: "List",
LocalConfig: userConfiguration,
Cmd: CmdListUsers,
ArgsFn: func() []string {
return []string{filename}
},
ArgsFn: listArgsFn(sess.Info().TeamID, "users"),
}
return w.Run(ctx)
}

func listArgsFn(teamID string, prefix string) func() []string {
return func() []string {
if filename == "" {
filename = makeFilename(prefix, teamID, extForType(commonFlags.listType))
}
return []string{filename}
}
}

func wizChannels(ctx context.Context, _ *base.Command, _ []string) error {
sess, err := bootstrap.SlackdumpSession(ctx)
if err != nil {
base.SetExitStatus(base.SInitializationError)
return err
}

filename = makeFilename("channels", sess.Info().TeamID, ".json")
filename = makeFilename("channels", sess.Info().TeamID, extForType(commonFlags.listType))
w := dumpui.Wizard{
Title: "List Channels",
Name: "List",
LocalConfig: chanFlags.configuration,
Cmd: CmdListChannels,
ArgsFn: func() []string {
return []string{filename}
},
ArgsFn: listArgsFn(sess.Info().TeamID, "channels"),
}
return w.Run(ctx)
}
Expand All @@ -59,7 +64,7 @@ func userConfiguration() cfgui.Configuration {
cfgui.ParamGroup{
Name: "User List Options",
Params: []cfgui.Parameter{
filenameParam("users.json"),
filenameParam("users" + extForType(commonFlags.listType)),
},
},
}
Expand All @@ -81,7 +86,7 @@ func (o *channelOptions) configuration() cfgui.Configuration {
cfgui.ParamGroup{
Name: "Channel Options",
Params: []cfgui.Parameter{
filenameParam("channels.json"),
filenameParam("channels" + extForType(commonFlags.listType)),
{
Name: "Resolve Users",
Value: cfgui.Checkbox(o.resolveUsers),
Expand Down
18 changes: 10 additions & 8 deletions internal/structures/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var ErrInvalidDomain = errors.New("invalid domain")

// ExtractWorkspace takes a workspace name or URL and returns the workspace name.
func ExtractWorkspace(workspace string) (string, error) {
if !strings.Contains(workspace, ".slack.com") && !strings.Contains(workspace, ".") {
if !strings.Contains(workspace, ".") {
return workspace, nil
}
if strings.HasPrefix(workspace, "https://") {
Expand All @@ -44,13 +44,15 @@ func ExtractWorkspace(workspace string) (string, error) {
}
workspace = uri.Host
}
// parse
name, domain, found := strings.Cut(workspace, ".")
if !found {
return "", errors.New("workspace name is empty")
if !strings.Contains(workspace, ".slack.com") {
return "", ErrInvalidDomain
}
if strings.TrimRight(domain, "/") != "slack.com" {
return "", fmt.Errorf("%s: %w", domain, ErrInvalidDomain)

parts := strings.Split(workspace, ".")
switch len(parts) {
case 3, 4:
return parts[0], nil
default:
return "", fmt.Errorf("invalid workspace: %s", workspace)
}
return name, nil
}
1 change: 1 addition & 0 deletions internal/structures/structures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestExtractWorkspace(t *testing.T) {
{"url no schema slash", args{"blahblah.slack.com/"}, "blahblah", false},
{"url no schema no slash", args{"blahblah.slack.com"}, "blahblah", false},
{"not a slack domain", args{"blahblah.example.com"}, "", true},
{"enterprise domain", args{"https://acme-co.enterprise.slack.com/"}, "acme-co", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 28971d8

Please sign in to comment.