From 9f5aee94285320dd725e02db39b8eb57cf6e0160 Mon Sep 17 00:00:00 2001 From: Cyril David Date: Wed, 20 Jan 2021 18:23:10 -0800 Subject: [PATCH] Add client list command Just exercising this quickly to see where it falls short. --- internal/cli/cli.go | 9 ------- internal/cli/clients.go | 48 +++++++++++++++++++++++++++++++++++++ internal/cli/root.go | 2 ++ internal/display/clients.go | 40 +++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 internal/cli/clients.go create mode 100644 internal/display/clients.go diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 0cbeb34f3..63a81b0a2 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -9,7 +9,6 @@ import ( "sync" "github.com/auth0/auth0-cli/internal/display" - "github.com/spf13/cobra" "gopkg.in/auth0.v5/management" ) @@ -105,11 +104,3 @@ func (c *cli) init() error { return err } - -func mustRequireFlags(cmd *cobra.Command, flags ...string) { - for _, f := range flags { - if err := cmd.MarkFlagRequired(f); err != nil { - panic(err) - } - } -} diff --git a/internal/cli/clients.go b/internal/cli/clients.go new file mode 100644 index 000000000..2a572d752 --- /dev/null +++ b/internal/cli/clients.go @@ -0,0 +1,48 @@ +package cli + +import ( + "github.com/auth0/auth0-cli/internal/ansi" + "github.com/spf13/cobra" + "gopkg.in/auth0.v5/management" +) + +func clientsCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "clients", + Short: "manage resources for clients.", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(listClientsCmd(cli)) + + return cmd +} + +func listClientsCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists your existing clients", + Long: `$ auth0 client list +Lists your existing clients. To create one try: + + $ auth0 client create +`, + RunE: func(cmd *cobra.Command, args []string) error { + var list *management.ClientList + + err := ansi.Spinner("Listing clients", func() (err error) { + list, err = cli.api.Client.List() + return err + }) + + if err != nil { + return err + } + + cli.renderer.ClientList(list.Clients) + return nil + }, + } + + return cmd +} diff --git a/internal/cli/root.go b/internal/cli/root.go index 16078df09..465536adb 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -44,6 +44,8 @@ func Execute() { rootCmd.PersistentFlags().BoolVar(&cli.verbose, "verbose", false, "Enable verbose mode.") + rootCmd.AddCommand(clientsCmd(cli)) + // TODO(cyx): backport this later on using latest auth0/v5. // rootCmd.AddCommand(actionsCmd(cli)) // rootCmd.AddCommand(triggersCmd(cli)) diff --git a/internal/display/clients.go b/internal/display/clients.go new file mode 100644 index 000000000..cdb41d56a --- /dev/null +++ b/internal/display/clients.go @@ -0,0 +1,40 @@ +package display + +import ( + "fmt" + + "github.com/auth0/auth0-cli/internal/ansi" + "gopkg.in/auth0.v5" + "gopkg.in/auth0.v5/management" +) + +func (r *Renderer) ClientList(clients []*management.Client) { + r.Heading(ansi.Bold(r.Tenant), "clients") + + for _, c := range clients { + fmt.Fprintf(r.Writer, "- %s (%s)\n", auth0.StringValue(c.Name), appTypeFor(c.AppType)) + fmt.Fprintf(r.Writer, " %s: %s\n", ansi.Italic("ClientID"), ansi.Faint(auth0.StringValue(c.ClientID))) + } +} + +func appTypeFor(v *string) string { + switch { + case v == nil: + return "generic" + + case *v == "non_interactive": + return "machine to machine" + + case *v == "native": + return "native" + + case *v == "spa": + return "single page application" + + case *v == "regular_web": + return "regular web application" + + default: + return *v + } +}