From d77eece5bda068c1863b8c6c00017e3b287d84d3 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 25 Jan 2021 15:58:20 -0300 Subject: [PATCH 1/4] feat: Add 'apis list' --- internal/auth/auth.go | 2 +- internal/cli/apis.go | 42 ++++++++++++++++++++++++++++++++++++++++ internal/cli/root.go | 1 + internal/display/apis.go | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 internal/cli/apis.go create mode 100644 internal/display/apis.go diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 0e74768a4..28084a41c 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -33,7 +33,7 @@ const ( deviceCodeEndpoint = "https://auth0.auth0.com/oauth/device/code" oauthTokenEndpoint = "https://auth0.auth0.com/oauth/token" // TODO(jfatta) extend the scope as we extend the CLI: - scope = "openid read:roles read:clients read:logs read:rules" + scope = "openid read:roles read:clients read:resource_servers read:logs read:rules" audiencePath = "/api/v2/" ) diff --git a/internal/cli/apis.go b/internal/cli/apis.go new file mode 100644 index 000000000..381f3b189 --- /dev/null +++ b/internal/cli/apis.go @@ -0,0 +1,42 @@ +package cli + +import ( + "github.com/spf13/cobra" + "gopkg.in/auth0.v5/management" +) + +func apisCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "apis", + Short: "manage resources for APIs.", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(listApisCmd(cli)) + + return cmd +} + +func listApisCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists your existing APIs", + Long: `$ auth0 apis list +Lists your existing APIs. To create one try: + + $ auth0 apis create +`, + RunE: func(cmd *cobra.Command, args []string) error { + list, err := cli.api.ResourceServer.List(management.IncludeTotals(false)) + + if err != nil { + return err + } + + cli.renderer.ApisList(list.ResourceServers) + return nil + }, + } + + return cmd +} diff --git a/internal/cli/root.go b/internal/cli/root.go index 585d3d491..c5b3837cb 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -52,6 +52,7 @@ func Execute() { rootCmd.AddCommand(loginCmd(cli)) rootCmd.AddCommand(clientsCmd(cli)) + rootCmd.AddCommand(apisCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(rulesCmd(cli)) diff --git a/internal/display/apis.go b/internal/display/apis.go new file mode 100644 index 000000000..c092c6a9c --- /dev/null +++ b/internal/display/apis.go @@ -0,0 +1,37 @@ +package display + +import ( + "github.com/auth0/auth0-cli/internal/ansi" + "gopkg.in/auth0.v5" + "gopkg.in/auth0.v5/management" +) + +type apiView struct { + ID string + Name string + Identifier string +} + +func (v *apiView) AsTableHeader() []string { + return []string{"ID", "Name", "Identifier"} +} + +func (v *apiView) AsTableRow() []string { + return []string{v.ID, v.Name, ansi.Faint(v.Identifier)} +} + +func (r *Renderer) ApisList(apis []*management.ResourceServer) { + r.Heading(ansi.Bold(r.Tenant), "APIs\n") + + var res []View + + for _, api := range apis { + res = append(res, &apiView{ + ID: auth0.StringValue(api.ID), + Name: appTypeFor(api.Name), + Identifier: auth0.StringValue(api.Identifier), + }) + } + + r.Results(res) +} From bb7786242c46e4f7cb779e2f603978ced1a0bd44 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 25 Jan 2021 16:00:38 -0300 Subject: [PATCH 2/4] Make the ID display faintly --- internal/display/apis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/display/apis.go b/internal/display/apis.go index c092c6a9c..5becdc929 100644 --- a/internal/display/apis.go +++ b/internal/display/apis.go @@ -17,7 +17,7 @@ func (v *apiView) AsTableHeader() []string { } func (v *apiView) AsTableRow() []string { - return []string{v.ID, v.Name, ansi.Faint(v.Identifier)} + return []string{ansi.Faint(v.ID), v.Name, v.Identifier} } func (r *Renderer) ApisList(apis []*management.ResourceServer) { From 85ddb616bdbfe106956a744cd6e7b0c7bbeb1ac3 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 25 Jan 2021 16:08:40 -0300 Subject: [PATCH 3/4] Use plain string value for the name --- internal/display/apis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/display/apis.go b/internal/display/apis.go index 5becdc929..208db8ecc 100644 --- a/internal/display/apis.go +++ b/internal/display/apis.go @@ -28,7 +28,7 @@ func (r *Renderer) ApisList(apis []*management.ResourceServer) { for _, api := range apis { res = append(res, &apiView{ ID: auth0.StringValue(api.ID), - Name: appTypeFor(api.Name), + Name: auth0.StringValue(api.Name), Identifier: auth0.StringValue(api.Identifier), }) } From d2dfe84e4330c0201e1c87e0c027c55b9c24a1aa Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 25 Jan 2021 16:10:12 -0300 Subject: [PATCH 4/4] Remove include totals --- internal/cli/apis.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/cli/apis.go b/internal/cli/apis.go index 381f3b189..ea51856a6 100644 --- a/internal/cli/apis.go +++ b/internal/cli/apis.go @@ -2,7 +2,6 @@ package cli import ( "github.com/spf13/cobra" - "gopkg.in/auth0.v5/management" ) func apisCmd(cli *cli) *cobra.Command { @@ -27,7 +26,7 @@ Lists your existing APIs. To create one try: $ auth0 apis create `, RunE: func(cmd *cobra.Command, args []string) error { - list, err := cli.api.ResourceServer.List(management.IncludeTotals(false)) + list, err := cli.api.ResourceServer.List() if err != nil { return err