Skip to content

Commit

Permalink
Merge pull request #4 from auth0/add-client-list-cmd
Browse files Browse the repository at this point in the history
Add client list command
  • Loading branch information
cyx authored Jan 21, 2021
2 parents 1e09143 + 5af3866 commit dfe8c78
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
9 changes: 0 additions & 9 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sync"

"github.com/auth0/auth0-cli/internal/display"
"github.com/spf13/cobra"
"gopkg.in/auth0.v5/management"
)

Expand Down Expand Up @@ -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)
}
}
}
40 changes: 40 additions & 0 deletions internal/cli/clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cli

import (
"github.com/spf13/cobra"
)

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 clients create
`,
RunE: func(cmd *cobra.Command, args []string) error {
list, err := cli.api.Client.List()
if err != nil {
return err
}

cli.renderer.ClientList(list.Clients)
return nil
},
}

return cmd
}
2 changes: 2 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
48 changes: 48 additions & 0 deletions internal/display/clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
if auth0.StringValue(c.Name) == deprecatedAppName {
continue
}

fmt.Fprintf(r.Writer, "- %s (%s)\n", auth0.StringValue(c.Name), appTypeFor(c.AppType))
fmt.Fprintf(r.Writer, " client id: %s\n", ansi.Faint(auth0.StringValue(c.ClientID)))
fmt.Fprintln(r.Writer)
}
}

// TODO(cyx): determine if there's a better way to filter this out.
const deprecatedAppName = "All Applications"

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
}
}

0 comments on commit dfe8c78

Please sign in to comment.