From 9cce8a8a3a5675cfb233fce51248bda79ef1305d Mon Sep 17 00:00:00 2001 From: Chris Opland Date: Thu, 25 Feb 2021 08:35:14 -0600 Subject: [PATCH] feat: add tenants use cmd (#109) --- internal/cli/cli.go | 23 +++++++++++++- internal/cli/root.go | 1 + internal/cli/tenants.go | 66 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 internal/cli/tenants.go diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 157a67d32..a5c07808c 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -130,6 +130,20 @@ func (c *cli) getTenant() (tenant, error) { return t, nil } +// listTenants fetches all of the configured tenants +func (c *cli) listTenants() ([]tenant, error) { + if err := c.init(); err != nil { + return []tenant{}, err + } + + tenants := make([]tenant, 0, len(c.config.Tenants)) + for _, t := range c.config.Tenants { + tenants = append(tenants, t) + } + + return tenants, nil +} + // addTenant assigns an existing, or new tenant. This is expected to be called // after a login has completed. func (c *cli) addTenant(ten tenant) error { @@ -151,6 +165,14 @@ func (c *cli) addTenant(ten tenant) error { c.config.Tenants[ten.Name] = ten + if err := c.persistConfig(); err != nil { + return fmt.Errorf("persisting config: %w", err) + } + + return nil +} + +func (c *cli) persistConfig() error { dir := filepath.Dir(c.path) if _, err := os.Stat(dir); os.IsNotExist(err) { if err := os.MkdirAll(dir, 0700); err != nil { @@ -166,7 +188,6 @@ func (c *cli) addTenant(ten tenant) error { if err := ioutil.WriteFile(c.path, buf, 0600); err != nil { return err } - return nil } diff --git a/internal/cli/root.go b/internal/cli/root.go index d1d550c76..47b15b6a3 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -60,6 +60,7 @@ func Execute() { // order of the comamnds here matters // so add new commands in a place that reflect its relevance or relation with other commands: rootCmd.AddCommand(loginCmd(cli)) + rootCmd.AddCommand(tenantsCmd(cli)) rootCmd.AddCommand(appsCmd(cli)) rootCmd.AddCommand(quickstartsCmd(cli)) rootCmd.AddCommand(apisCmd(cli)) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go new file mode 100644 index 000000000..843a6a2ec --- /dev/null +++ b/internal/cli/tenants.go @@ -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 `, + 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 +}