diff --git a/internal/cli/cli.go b/internal/cli/cli.go index f788f81c2..cd819bc4e 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -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 diff --git a/internal/cli/logout.go b/internal/cli/logout.go index a9699f25b..3c63fc141 100644 --- a/internal/cli/logout.go +++ b/internal/cli/logout.go @@ -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 { @@ -19,42 +16,16 @@ func logoutCmd(cli *cli) *cobra.Command { auth0 logout 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 }, } diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index 44f7a404c..1862188a9 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -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", @@ -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 } @@ -59,35 +63,16 @@ func useTenantCmd(cli *cli) *cobra.Command { auth0 tenants use 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 }, @@ -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), @@ -115,20 +91,12 @@ func openTenantCmd(cli *cli) *cobra.Command { auth0 tenants open 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 }, } @@ -136,19 +104,37 @@ func openTenantCmd(cli *cli) *cobra.Command { 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) @@ -156,7 +142,7 @@ func (c *cli) tenantPickerOptions() (pickerOptions, error) { } 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