diff --git a/internal/cli/users.go b/internal/cli/users.go index e6790b22e..65d797f8f 100644 --- a/internal/cli/users.go +++ b/internal/cli/users.go @@ -68,6 +68,12 @@ var ( ShortForm: "s", Help: "Field to sort by. Use 'field:order' where 'order' is '1' for ascending and '-1' for descending. e.g. 'created_at:1'.", } + userNumber = Flag{ + Name: "Number", + LongForm: "number", + ShortForm: "n", + Help: "Number of users, that match the search criteria, to retrieve. Maximum result number is 1000. If limit is hit, please refine the search query.", + } userImportTemplate = Flag{ Name: "Template", LongForm: "template", @@ -127,8 +133,9 @@ func usersCmd(cli *cli) *cobra.Command { func searchUsersCmd(cli *cli) *cobra.Command { var inputs struct { - query string - sort string + query string + sort string + number int } cmd := &cobra.Command{ @@ -139,34 +146,54 @@ func searchUsersCmd(cli *cli) *cobra.Command { Example: ` auth0 users search auth0 users search --query id auth0 users search -q name --sort "name:1" - auth0 users search -q name -s "name:1" --json`, + auth0 users search -q name -s "name:1" --number 200 + auth0 users search -q name -s "name:1" -n 200 --json`, RunE: func(cmd *cobra.Command, args []string) error { if err := userQuery.Ask(cmd, &inputs.query, nil); err != nil { return err } - search := &management.UserList{} + queryParams := []management.RequestOption{ + management.Query(inputs.query), + } + if inputs.sort != "" { + queryParams = append(queryParams, management.Parameter("sort", inputs.sort)) + } + + if inputs.number < 1 || inputs.number > 1000 { + return fmt.Errorf("number flag invalid, please pass a number between 1 and 1000") + } - var queryParams []management.RequestOption + list, err := getWithPagination( + cmd.Context(), + inputs.number, + func(opts ...management.RequestOption) (result []interface{}, hasNext bool, err error) { + opts = append(opts, queryParams...) - if len(inputs.sort) == 0 { - queryParams = append(queryParams, management.Query(auth0.StringValue(&inputs.query))) - } else { - queryParams = append(queryParams, - management.Query(auth0.StringValue(&inputs.query)), - management.Parameter("sort", auth0.StringValue(&inputs.sort)), - ) + userList, err := cli.api.User.Search(opts...) + if err != nil { + return nil, false, err + } + + var output []interface{} + for _, user := range userList.Users { + output = append(output, user) + } + + return output, userList.HasNext(), nil + }, + ) + if err != nil { + return fmt.Errorf("failed to search for users: %w", err) } - if err := ansi.Waiting(func() error { - var err error - search, err = cli.api.User.Search(queryParams...) - return err - }); err != nil { - return fmt.Errorf("An unexpected error occurred: %w", err) + var foundUsers []*management.User + for _, item := range list { + foundUsers = append(foundUsers, item.(*management.User)) } - cli.renderer.UserSearch(search.Users) + cli.renderer.UserSearch(foundUsers) + return nil }, } @@ -174,6 +201,7 @@ func searchUsersCmd(cli *cli) *cobra.Command { cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.") userQuery.RegisterString(cmd, &inputs.query, "") userSort.RegisterString(cmd, &inputs.sort, "") + userNumber.RegisterInt(cmd, &inputs.number, defaultPageSize) return cmd } diff --git a/test/integration/scripts/get-user-id.sh b/test/integration/scripts/get-user-id.sh index 2963b22b7..a447b3d9f 100755 --- a/test/integration/scripts/get-user-id.sh +++ b/test/integration/scripts/get-user-id.sh @@ -1,6 +1,6 @@ #! /bin/bash -user=$( auth0 users create -n integration-test-user-better -c Username-Password-Authentication -e newuser@example.com -p testUser12 --json --no-input ) +user=$( auth0 users create -n integration-test-user-better -c Username-Password-Authentication -e newuser@example.com -p testUser12 -u cli-test --json --no-input ) mkdir -p ./test/integration/identifiers echo "$user" | jq -r '.["user_id"]' > ./test/integration/identifiers/user-id diff --git a/test/integration/test-cases.yaml b/test/integration/test-cases.yaml index 255573f57..40acad049 100644 --- a/test/integration/test-cases.yaml +++ b/test/integration/test-cases.yaml @@ -399,7 +399,7 @@ tests: # Test 'users create' users create and check data: - command: auth0 users create --name integration-test-user-new --connection Username-Password-Authentication --email testuser@example.com --password testUser12 --json --no-input + command: auth0 users create --name integration-test-user-new --connection Username-Password-Authentication --email testuser@example.com --password testUser12 --username testuser1 --json --no-input exit-code: 0 stdout: json: @@ -407,7 +407,7 @@ tests: connection: "Username-Password-Authentication" users create and check output: - command: auth0 users create --name integration-test-user-new2 --connection Username-Password-Authentication --email testuser2@example.com --password testUser12 --no-input + command: auth0 users create --name integration-test-user-new2 --connection Username-Password-Authentication --email testuser2@example.com --password testUser12 --username testuser2 --no-input exit-code: 0 stdout: contains: @@ -435,6 +435,13 @@ tests: - CONNECTION Username-Password-Authentication exit-code: 0 + users search: + command: auth0 users search --query user_id:"$(cat ./test/integration/identifiers/user-id)" --number 1 --sort "name:-1" + exit-code: 0 + stdout: + contains: + - newuser@example.com + # Test 'users update' users update email: command: auth0 users update $(cat ./test/integration/identifiers/user-id) --email betteruser@example.com --json --no-input