-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/auth0/auth0-cli/internal/prompt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func tenantsCmd(cli *cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "tenants", | ||
Short: "Manage configured tenants", | ||
} | ||
|
||
cmd.AddCommand(useTenantCmd(cli)) | ||
return cmd | ||
} | ||
|
||
func useTenantCmd(cli *cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "use", | ||
Aliases: []string{"select"}, | ||
Short: "Set the active tenant", | ||
Long: `auth0 tenants use <tenant>`, | ||
Args: cobra.MaximumNArgs(1), | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
prepareInteractivity(cmd) | ||
}, | ||
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 from config") | ||
} | ||
|
||
tenNames := make([]string, len(tens)) | ||
for i, t := range tens { | ||
tenNames[i] = t.Name | ||
} | ||
|
||
input := prompt.SelectInput("tenant", "Tenant:", "Tenant to activate", tenNames, true) | ||
if err := prompt.AskOne(input, &selectedTenant); err != nil { | ||
return err | ||
} | ||
} else { | ||
requestedTenant := args[0] | ||
t, ok := cli.config.Tenants[requestedTenant] | ||
if !ok { | ||
return fmt.Errorf("Unable to find tenant in config: %s", requestedTenant) | ||
|
||
} | ||
selectedTenant = t.Name | ||
} | ||
|
||
cli.config.DefaultTenant = selectedTenant | ||
if err := cli.persistConfig(); err != nil { | ||
return fmt.Errorf("persisting config: %w", err) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |