Skip to content

Commit

Permalink
Merge pull request #77 from auth0/feature/A0CLI-42
Browse files Browse the repository at this point in the history
Basic user get/search command
  • Loading branch information
cyx authored Jan 27, 2021
2 parents 6f9e3d5 + 209afec commit c283847
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var requiredScopes = []string{
"read:client_keys", "read:logs",
"create:roles", "delete:roles", "read:roles", "update:roles",
"create:custom_domains", "delete:custom_domains", "read:custom_domains", "update:custom_domains",
"read:users",
}

type Authenticator struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/auth0/auth0.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type API struct {
ResourceServer ResourceServerAPI
Role RoleAPI
CustomDomain CustomDomainAPI
User UserAPI
}

func NewAPI(m *management.Management) *API {
Expand All @@ -32,6 +33,7 @@ func NewAPI(m *management.Management) *API {
Rule: m.Rule,
Role: m.Role,
CustomDomain: m.CustomDomain,
User: m.User,
}
}

Expand Down
13 changes: 13 additions & 0 deletions internal/auth0/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:generate mockgen -source=user.go -destination=user_mock.go -package=auth0

package auth0

import "gopkg.in/auth0.v5/management"

type UserAPI interface {
// Read a user by its id.
Read(id string, opts ...management.RequestOption) (u *management.User, err error)

// List users by email.
ListByEmail(email string, opts ...management.RequestOption) (us []*management.User, err error)
}
150 changes: 150 additions & 0 deletions internal/auth0/user_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func Execute() {
rootCmd.AddCommand(rolesCmd(cli))
rootCmd.AddCommand(customDomainsCmd(cli))
rootCmd.AddCommand(getTokenCmd(cli))
rootCmd.AddCommand(usersCmd(cli))

// keep completion at the bottom:
rootCmd.AddCommand(completionCmd(cli))
Expand Down
98 changes: 98 additions & 0 deletions internal/cli/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cli

import (
"errors"

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

func usersCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "users",
Short: "manage users.",
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(getusersCmd(cli))

return cmd
}

func getusersCmd(cli *cli) *cobra.Command {
var flags struct {
id string
email string
fields string
}

cmd := &cobra.Command{
Use: "get",
Short: "Get a user's details",
Long: `$ auth0 users get
Get a user
`,
RunE: func(cmd *cobra.Command, args []string) error {
userID, err := cmd.LocalFlags().GetString("id")
if err != nil {
return err
}

userEmail, err := cmd.LocalFlags().GetString("email")
if err != nil {
return err
}

if userID == "" && userEmail == "" {
return errors.New("User id or email flag must be specified")
}

if userID != "" && userEmail != "" {
return errors.New("User id and email flags cannot be combined")
}

var users []*management.User
var user *management.User

if userID != "" {
err := ansi.Spinner("Getting user", func() error {
var err error
user, err = cli.api.User.Read(flags.id)
return err
})

if err != nil {
return err
}

users = append(users, user)

cli.renderer.UserList(users)
return nil
}

if userEmail != "" {
err := ansi.Spinner("Getting user(s)", func() error {
var err error
users, err = cli.api.User.ListByEmail(userEmail)
return err
})

if err != nil {
return err
}

cli.renderer.UserList(users)
return nil
}

return nil
},
}

cmd.Flags().StringVarP(&flags.id, "id", "i", "", "User ID of user to get.")
cmd.Flags().StringVarP(&flags.email, "email", "e", "", "Email of user to get.")

return cmd
}
39 changes: 39 additions & 0 deletions internal/display/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package display

import (
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"gopkg.in/auth0.v5/management"
)

type userView struct {
UserID string
Connection string
Name string
Email string
LatestLogin string
}

func (v *userView) AsTableHeader() []string {
return []string{"User ID", "Name", "Email", "Latest Login"}
}

func (v *userView) AsTableRow() []string {
return []string{ansi.Faint(v.UserID), v.Name, v.Email, v.LatestLogin}
}

func (r *Renderer) UserList(users []*management.User) {
r.Heading(ansi.Bold(r.Tenant), "users\n")

var res []View
for _, u := range users {
res = append(res, &userView{
UserID: auth0.StringValue(u.ID),
Name: auth0.StringValue(u.Name),
Email: auth0.StringValue(u.Email),
LatestLogin: u.LastLogin.String(),
})
}

r.Results(res)
}

0 comments on commit c283847

Please sign in to comment.