Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic user get/search command #77

Merged
merged 1 commit into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we wanna add the rest of CRUD now?

}

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 @@ -68,6 +68,7 @@ func Execute() {
rootCmd.AddCommand(rolesCmd(cli))
rootCmd.AddCommand(customDomainsCmd(cli))
rootCmd.AddCommand(getTokenCmd(cli))
rootCmd.AddCommand(usersCmd(cli))

// TODO(cyx): backport this later on using latest auth0/v5.
// rootCmd.AddCommand(actionsCmd(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)
}