Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tenants use cmd #109

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}