Skip to content

Commit

Permalink
feat: add tenants use cmd (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
copland authored Feb 25, 2021
1 parent f32f8e6 commit 9cce8a8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
23 changes: 22 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
66 changes: 66 additions & 0 deletions internal/cli/tenants.go
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
}

0 comments on commit 9cce8a8

Please sign in to comment.