From 9cc229538cf48c2ffaf0f86bca52d00252ca0437 Mon Sep 17 00:00:00 2001 From: Chris Opland Date: Wed, 24 Feb 2021 20:40:10 -0600 Subject: [PATCH 1/3] feat: add tenants use cmd --- internal/cli/cli.go | 21 +++++++++++++- internal/cli/root.go | 1 + internal/cli/tenants.go | 63 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 internal/cli/tenants.go diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 157a67d32..e59bd82ae 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,12 @@ func (c *cli) addTenant(ten tenant) error { c.config.Tenants[ten.Name] = ten + c.persistConfig() + + 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 +186,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..11ecdac7e 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -66,6 +66,7 @@ func Execute() { rootCmd.AddCommand(testCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(actionsCmd(cli)) + rootCmd.AddCommand(tenantsCmd(cli)) // keep completion at the bottom: rootCmd.AddCommand(completionCmd(cli)) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go new file mode 100644 index 000000000..db092144d --- /dev/null +++ b/internal/cli/tenants.go @@ -0,0 +1,63 @@ +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", + 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 + cli.persistConfig() + return nil + }, + } + + return cmd +} From 63a0c150f89fc31c841f3588113292b839ffaa69 Mon Sep 17 00:00:00 2001 From: Chris Opland Date: Wed, 24 Feb 2021 20:44:51 -0600 Subject: [PATCH 2/3] fix lint issues --- internal/cli/cli.go | 4 +++- internal/cli/tenants.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index e59bd82ae..a5c07808c 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -165,7 +165,9 @@ func (c *cli) addTenant(ten tenant) error { c.config.Tenants[ten.Name] = ten - c.persistConfig() + if err := c.persistConfig(); err != nil { + return fmt.Errorf("persisting config: %w", err) + } return nil } diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index db092144d..e147a5942 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -54,7 +54,9 @@ func useTenantCmd(cli *cli) *cobra.Command { } cli.config.DefaultTenant = selectedTenant - cli.persistConfig() + if err := cli.persistConfig(); err != nil { + return fmt.Errorf("persisting config: %w", err) + } return nil }, } From 96a5d33bdac232fdd405a122694cf6963dbc5942 Mon Sep 17 00:00:00 2001 From: Chris Opland Date: Thu, 25 Feb 2021 08:05:23 -0600 Subject: [PATCH 3/3] rearrange cmds; add select alias --- internal/cli/root.go | 2 +- internal/cli/tenants.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/cli/root.go b/internal/cli/root.go index 11ecdac7e..47b15b6a3 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -60,13 +60,13 @@ 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)) rootCmd.AddCommand(testCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(actionsCmd(cli)) - rootCmd.AddCommand(tenantsCmd(cli)) // keep completion at the bottom: rootCmd.AddCommand(completionCmd(cli)) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index e147a5942..843a6a2ec 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -19,10 +19,11 @@ func tenantsCmd(cli *cli) *cobra.Command { func useTenantCmd(cli *cli) *cobra.Command { cmd := &cobra.Command{ - Use: "use", - Short: "Set the active tenant", - Long: `auth0 tenants use `, - Args: cobra.MaximumNArgs(1), + 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) },