From 16400bd18c137be75ddd46cc3932585ba0962e7a Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Mon, 25 Jan 2021 15:31:11 -0500 Subject: [PATCH 1/2] Add `connections` and `connections list`. --- internal/cli/connections.go | 40 +++++++++++++++++++++++++++++++++ internal/cli/root.go | 1 + internal/display/connections.go | 37 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 internal/cli/connections.go create mode 100644 internal/display/connections.go diff --git a/internal/cli/connections.go b/internal/cli/connections.go new file mode 100644 index 000000000..6090c3838 --- /dev/null +++ b/internal/cli/connections.go @@ -0,0 +1,40 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +func connectionsCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "connections", + Short: "manage resources for connections.", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(listConnectionsCmd(cli)) + + return cmd +} + +func listConnectionsCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists your existing connections", + Long: `$ auth0 connections list +Lists your existing connections. To create one try: + + $ auth0 connections create +`, + RunE: func(cmd *cobra.Command, args []string) error { + list, err := cli.api.Connection.List() + if err != nil { + return err + } + + cli.renderer.ConnectionList(list.Connections) + return nil + }, + } + + return cmd +} diff --git a/internal/cli/root.go b/internal/cli/root.go index 73ef279b2..5e7bd5b47 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -56,6 +56,7 @@ func Execute() { rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(actionsCmd(cli)) rootCmd.AddCommand(rulesCmd(cli)) + rootCmd.AddCommand(connectionsCmd(cli)) // TODO(cyx): backport this later on using latest auth0/v5. // rootCmd.AddCommand(actionsCmd(cli)) diff --git a/internal/display/connections.go b/internal/display/connections.go new file mode 100644 index 000000000..6e80bc45c --- /dev/null +++ b/internal/display/connections.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 connectionView struct { + Name string + Strategy string + ConnectionID string +} + +func (v *connectionView) AsTableHeader() []string { + return []string{"Name", "Type", "ClientID"} +} + +func (v *connectionView) AsTableRow() []string { + return []string{v.Name, v.Strategy, ansi.Faint(v.ConnectionID)} +} + +func (r *Renderer) ConnectionList(connections []*management.Connection) { + r.Heading(ansi.Bold(r.Tenant), "connections\n") + + var res []View + for _, c := range connections { + res = append(res, &connectionView{ + Name: auth0.StringValue(c.Name), + Strategy: auth0.StringValue(c.Strategy), + ConnectionID: auth0.StringValue(c.ID), + }) + + } + + r.Results(res) +} From ce8e51991af5569f3cc2dacda8a286ef2c7d5dd7 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Mon, 25 Jan 2021 15:34:57 -0500 Subject: [PATCH 2/2] Add scope for reading connections --- internal/auth/auth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index ae922ed96..7e86257c2 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 create:actions create:clients create:connections create:hooks create:rules delete:actions delete:clients delete:connections delete:hooks delete:rules read:actions read:clients read:connections read:hooks read:logs read:rules update:actions update:clients update:connections update:hooks update:rules" + scope = "openid create:actions create:clients create:connections create:hooks create:rules delete:actions delete:clients delete:connections delete:hooks delete:rules read:actions read:clients read:connections read:hooks read:logs read:rules update:actions update:clients update:connections update:hooks update:rules read:connections" audiencePath = "/api/v2/" )