Skip to content

Commit

Permalink
DXCDT-319: Refactor how we select the tenant domain for multiple cmds (
Browse files Browse the repository at this point in the history
…#612)

* Refactor how we select the tenant domain for multiple cmds

* Rename selectTenant to selectValidTenanatFromConfig

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
sergiught and willvedd authored Jan 18, 2023
1 parent abe6c4c commit 0919796
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 96 deletions.
2 changes: 1 addition & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (c *cli) getTenant() (Tenant, error) {
return t, nil
}

// listTenants fetches all of the configured tenants.
// listTenants fetches all the configured tenants.
func (c *cli) listTenants() ([]Tenant, error) {
if err := c.init(); err != nil {
return []Tenant{}, err
Expand Down
39 changes: 5 additions & 34 deletions internal/cli/logout.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package cli

import (
"errors"
"fmt"

"github.com/spf13/cobra"

"github.com/auth0/auth0-cli/internal/prompt"
)

func logoutCmd(cli *cli) *cobra.Command {
Expand All @@ -19,42 +16,16 @@ func logoutCmd(cli *cli) *cobra.Command {
auth0 logout <tenant>
auth0 logout "example.us.auth0.com"`,
RunE: func(cmd *cobra.Command, args []string) error {
// NOTE(cyx): This was mostly copy/pasted from tenants
// use command. Consider refactoring.
var selectedTenant string
if len(args) == 0 {
tens, err := cli.listTenants()
if err != nil {
return err // This error is already formatted for display
}

if len(tens) == 0 {
return errors.New("there are no tenants available to perform the logout")
}

tenNames := make([]string, len(tens))
for i, t := range tens {
tenNames[i] = t.Domain
}

input := prompt.SelectInput("tenant", "Tenant:", "Tenant to logout", tenNames, tenNames[0], true)
if err := prompt.AskOne(input, &selectedTenant); err != nil {
return handleInputError(err)
}
} else {
requestedTenant := args[0]
t, ok := cli.config.Tenants[requestedTenant]
if !ok {
return fmt.Errorf("Unable to find tenant %s; run 'auth0 tenants use' to see your configured tenants or run 'auth0 login' to configure a new tenant", requestedTenant)
}
selectedTenant = t.Domain
selectedTenant, err := selectValidTenantFromConfig(cli, cmd, args)
if err != nil {
return err
}

if err := cli.removeTenant(selectedTenant); err != nil {
return err
return fmt.Errorf("failed to log out from the tenant %s: %w", selectedTenant, err)
}

cli.renderer.Infof("Successfully logged out tenant: %s", selectedTenant)
cli.renderer.Infof("Successfully logged out from tenant: %s", selectedTenant)
return nil
},
}
Expand Down
108 changes: 47 additions & 61 deletions internal/cli/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"

"github.com/spf13/cobra"

"github.com/auth0/auth0-cli/internal/prompt"
)

var tenantDomain = Argument{
Name: "Tenant",
Help: "Tenant to select",
}

func tenantsCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "tenants",
Expand All @@ -32,20 +35,21 @@ func listTenantCmd(cli *cli) *cobra.Command {
Example: ` auth0 tenants list
auth0 tenants ls`,
RunE: func(cmd *cobra.Command, args []string) error {
tens, err := cli.listTenants()
tenants, err := cli.listTenants()
if err != nil {
return fmt.Errorf("Unable to load tenants due to an unexpected error: %w", err)
return fmt.Errorf("failed to load tenants: %w", err)
}

tenNames := make([]string, len(tens))
for i, t := range tens {
tenNames[i] = t.Domain
tenantNames := make([]string, len(tenants))
for i, t := range tenants {
tenantNames[i] = t.Domain
}

cli.renderer.TenantList(tenNames)
cli.renderer.TenantList(tenantNames)
return nil
},
}

return cmd
}

Expand All @@ -59,35 +63,16 @@ func useTenantCmd(cli *cli) *cobra.Command {
auth0 tenants use <tenant>
auth0 tenants use "example.us.auth0.com"`,
RunE: func(cmd *cobra.Command, args []string) error {
var selectedTenant string
if len(args) == 0 {
tens, err := cli.listTenants()
if err != nil {
return fmt.Errorf("Unable to load tenants due to an unexpected error: %w", err)
}

tenNames := make([]string, len(tens))
for i, t := range tens {
tenNames[i] = t.Domain
}

input := prompt.SelectInput("tenant", "Tenant:", "Tenant to activate", tenNames, tenNames[0], true)
if err := prompt.AskOne(input, &selectedTenant); err != nil {
return handleInputError(err)
}
} else {
requestedTenant := args[0]
t, ok := cli.config.Tenants[requestedTenant]
if !ok {
return fmt.Errorf("Unable to find tenant %s; run 'auth0 tenants use' to see your configured tenants or run 'auth0 login' to configure a new tenant", requestedTenant)
}
selectedTenant = t.Domain
selectedTenant, err := selectValidTenantFromConfig(cli, cmd, args)
if err != nil {
return err
}

cli.config.DefaultTenant = selectedTenant
if err := cli.persistConfig(); err != nil {
return fmt.Errorf("An error occurred while setting the default tenant: %w", err)
return fmt.Errorf("failed to set the default tenant: %w", err)
}

cli.renderer.Infof("Default tenant switched to: %s", selectedTenant)
return nil
},
Expand All @@ -97,15 +82,6 @@ func useTenantCmd(cli *cli) *cobra.Command {
}

func openTenantCmd(cli *cli) *cobra.Command {
var inputs struct {
Domain string
}

var tenantDomain = Argument{
Name: "Tenant",
Help: "Tenant to select",
}

cmd := &cobra.Command{
Use: "open",
Args: cobra.MaximumNArgs(1),
Expand All @@ -115,48 +91,58 @@ func openTenantCmd(cli *cli) *cobra.Command {
auth0 tenants open <tenant>
auth0 tenants open "example.us.auth0.com"`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := tenantDomain.Pick(cmd, &inputs.Domain, cli.tenantPickerOptions)
if err != nil {
return err
}
} else {
inputs.Domain = args[0]

if _, ok := cli.config.Tenants[inputs.Domain]; !ok {
return fmt.Errorf("Unable to find tenant %s; run 'auth0 login' to configure a new tenant", inputs.Domain)
}
selectedTenant, err := selectValidTenantFromConfig(cli, cmd, args)
if err != nil {
return err
}

openManageURL(cli, inputs.Domain, "tenant/general")
openManageURL(cli, selectedTenant, "tenant/general")
return nil
},
}

return cmd
}

func selectValidTenantFromConfig(cli *cli, cmd *cobra.Command, args []string) (string, error) {
var selectedTenant string

if len(args) == 0 {
err := tenantDomain.Pick(cmd, &selectedTenant, cli.tenantPickerOptions)
return selectedTenant, err
}

selectedTenant = args[0]
if _, ok := cli.config.Tenants[selectedTenant]; !ok {
return "", fmt.Errorf(
"failed to find tenant %s.\n\nRun 'auth0 login' to configure a new tenant.",
selectedTenant,
)
}

return selectedTenant, nil
}

func (c *cli) tenantPickerOptions() (pickerOptions, error) {
tens, err := c.listTenants()
tenants, err := c.listTenants()
if err != nil {
return nil, fmt.Errorf("Unable to load tenants due to an unexpected error: %w", err)
return nil, fmt.Errorf("failed to load tenants: %w", err)
}

var priorityOpts, opts pickerOptions
for _, tenant := range tenants {
opt := pickerOption{value: tenant.Domain, label: tenant.Domain}

for _, t := range tens {
opt := pickerOption{value: t.Domain, label: t.Domain}

// check if this is currently the default tenant.
if t.Domain == c.config.DefaultTenant {
// Check if this is currently the default tenant.
if tenant.Domain == c.config.DefaultTenant {
priorityOpts = append(priorityOpts, opt)
} else {
opts = append(opts, opt)
}
}

if len(opts)+len(priorityOpts) == 0 {
return nil, errNoApps
return nil, fmt.Errorf("there are currently no tenants to pick from")
}

return append(priorityOpts, opts...), nil
Expand Down

0 comments on commit 0919796

Please sign in to comment.