Skip to content

Commit

Permalink
feat: paginate apps list (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfatta authored Sep 10, 2021
1 parent a3705d4 commit 0b85c1b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
60 changes: 47 additions & 13 deletions internal/cli/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
appTypeRegularWeb = "regular_web"
appTypeNonInteractive = "non_interactive"
appDefaultURL = "http://localhost:3000"
defaultPageSize = 50
)

var (
Expand Down Expand Up @@ -112,10 +113,16 @@ var (
IsRequired: false,
}
reveal = Flag{
Name: "Reveal",
LongForm: "reveal",
ShortForm: "r",
Help: "Display the Client Secret as part of the command output.",
Name: "Reveal",
LongForm: "reveal",
ShortForm: "r",
Help: "Display the Client Secret as part of the command output.",
}
number = Flag{
Name: "Number",
LongForm: "number",
ShortForm: "n",
Help: "Number of apps to retrieve",
}
)

Expand Down Expand Up @@ -185,9 +192,11 @@ func useAppCmd(cli *cli) *cobra.Command {
}

func listAppsCmd(cli *cli) *cobra.Command {
var inputs struct{
var inputs struct {
Reveal bool
Number int
}

cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Expand All @@ -196,25 +205,50 @@ func listAppsCmd(cli *cli) *cobra.Command {
Long: `List your existing applications. To create one try:
auth0 apps create`,
Example: `auth0 apps list
auth0 apps ls`,
auth0 apps ls
auth0 apps ls -n 100`,
RunE: func(cmd *cobra.Command, args []string) error {

var list *management.ClientList

var list []*management.Client
if err := ansi.Waiting(func() error {
var err error
list, err = cli.api.Client.List()
return err
pageSize := defaultPageSize
page := 0
for {
if inputs.Number > 0 {
// determine page size to avoid getting unwanted elements
want := inputs.Number - int(len(list))
if want == 0 {
return nil
}
if want < defaultPageSize {
pageSize = want
} else {
pageSize = defaultPageSize
}
}
res, err := cli.api.Client.List(
management.Context(cmd.Context()),
management.PerPage(pageSize),
management.Page(page))
if err != nil {
return err
}
page++
list = append(list, res.Clients...)
if len(list) == inputs.Number || !res.List.HasNext() {
return nil
}
}
}); err != nil {
return fmt.Errorf("An unexpected error occurred: %w", err)
}

cli.renderer.ApplicationList(list.Clients, inputs.Reveal)
cli.renderer.ApplicationList(list, inputs.Reveal)
return nil
},
}

reveal.RegisterBool(cmd, &inputs.Reveal, false)
number.RegisterInt(cmd, &inputs.Number, defaultPageSize)

return cmd
}
Expand Down
2 changes: 1 addition & 1 deletion internal/display/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (v *applicationView) Object() interface{} {
func (r *Renderer) ApplicationList(clients []*management.Client, revealSecrets bool) {
resource := "applications"

r.Heading(resource)
r.Heading(fmt.Sprintf("%s (%v)", resource, len(clients)))

if len(clients) == 0 {
r.EmptyState(resource)
Expand Down

0 comments on commit 0b85c1b

Please sign in to comment.