From a27696955f47090f2b25613627033b5bf7093674 Mon Sep 17 00:00:00 2001 From: bright-poku Date: Mon, 15 Mar 2021 08:26:42 -0400 Subject: [PATCH 1/4] [CLI-62] cli command to list tenants available --- internal/cli/tenants.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index 03476e381..e37db38eb 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -2,6 +2,8 @@ package cli import ( "fmt" + "github.com/olekukonko/tablewriter" + "os" "github.com/auth0/auth0-cli/internal/prompt" "github.com/spf13/cobra" @@ -14,9 +16,41 @@ func tenantsCmd(cli *cli) *cobra.Command { } cmd.AddCommand(useTenantCmd(cli)) + cmd.AddCommand(listTenantCmd(cli)) return cmd } +func listTenantCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List your tenants", + Long: `auth0 tenants list`, + RunE: func(cmd *cobra.Command, args []string) error { + listTenant, err := cli.listTenants() + if err != nil { + return fmt.Errorf("Unable to load tenants due to an unexpected error: %w", err) + } + + tenNames := make([]string, len(listTenant)) + for i, t := range listTenant { + tenNames[i] = t.Name + } + + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Available tenants"}) + for _, val := range tenNames { + b := []string{val} + table.Append(b) + } + + table.Render() + return nil + }, + } + return cmd +} + + func useTenantCmd(cli *cli) *cobra.Command { cmd := &cobra.Command{ Use: "use", From 73724b21b9e6954bc53747b2aef431958e280f81 Mon Sep 17 00:00:00 2001 From: bright-poku Date: Mon, 15 Mar 2021 08:36:30 -0400 Subject: [PATCH 2/4] [CLI-62] reformat imports --- internal/cli/tenants.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index e37db38eb..bc4c465f4 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -2,9 +2,9 @@ package cli import ( "fmt" - "github.com/olekukonko/tablewriter" "os" + "github.com/olekukonko/tablewriter" "github.com/auth0/auth0-cli/internal/prompt" "github.com/spf13/cobra" ) @@ -39,8 +39,8 @@ func listTenantCmd(cli *cli) *cobra.Command { table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Available tenants"}) for _, val := range tenNames { - b := []string{val} - table.Append(b) + res := []string{val} + table.Append(res) } table.Render() From fd9fa3842b1d349b2df4521bcb8c9a031bb1186d Mon Sep 17 00:00:00 2001 From: bright-poku Date: Mon, 15 Mar 2021 16:39:29 -0400 Subject: [PATCH 3/4] [CLI-62] add alias ls to the list command and display logic --- internal/cli/tenants.go | 19 +++++-------------- internal/display/tenants.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 internal/display/tenants.go diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index bc4c465f4..d9956f872 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -2,9 +2,6 @@ package cli import ( "fmt" - "os" - - "github.com/olekukonko/tablewriter" "github.com/auth0/auth0-cli/internal/prompt" "github.com/spf13/cobra" ) @@ -25,25 +22,19 @@ func listTenantCmd(cli *cli) *cobra.Command { Use: "list", Short: "List your tenants", Long: `auth0 tenants list`, + Aliases: []string{"ls"}, RunE: func(cmd *cobra.Command, args []string) error { - listTenant, err := cli.listTenants() + tens, err := cli.listTenants() if err != nil { return fmt.Errorf("Unable to load tenants due to an unexpected error: %w", err) } - tenNames := make([]string, len(listTenant)) - for i, t := range listTenant { + tenNames := make([]string, len(tens)) + for i, t := range tens { tenNames[i] = t.Name } - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"Available tenants"}) - for _, val := range tenNames { - res := []string{val} - table.Append(res) - } - - table.Render() + cli.renderer.ShowTenants(tenNames) return nil }, } diff --git a/internal/display/tenants.go b/internal/display/tenants.go new file mode 100644 index 000000000..eb7e02966 --- /dev/null +++ b/internal/display/tenants.go @@ -0,0 +1,26 @@ +package display + +//import "gopkg.in/auth0.v5" + +type tenantView struct { + Name string +} + +func (v *tenantView) AsTableHeader() []string { + return []string{"Available tenants"} +} + +func (v *tenantView) AsTableRow() []string { + return []string{v.Name} +} + +func (r *Renderer) ShowTenants(data []string) { + var results []View + for _, item := range data { + results = append(results, &tenantView{ + Name: item, + }) + } + + r.Results(results) +} From 18e86177824db0b508f348cf1339a35fe55c608d Mon Sep 17 00:00:00 2001 From: bright-poku Date: Mon, 15 Mar 2021 17:14:32 -0400 Subject: [PATCH 4/4] [CLI-62] cleaup --- internal/display/tenants.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/display/tenants.go b/internal/display/tenants.go index eb7e02966..fcc968752 100644 --- a/internal/display/tenants.go +++ b/internal/display/tenants.go @@ -1,7 +1,5 @@ package display -//import "gopkg.in/auth0.v5" - type tenantView struct { Name string }