From 0b85c1b914ff89d32647462328f611bbdf039ddd Mon Sep 17 00:00:00 2001 From: "Jorge L. Fatta" Date: Fri, 10 Sep 2021 17:55:15 -0300 Subject: [PATCH] feat: paginate apps list (#353) --- internal/cli/apps.go | 60 +++++++++++++++++++++++++++++++--------- internal/display/apps.go | 2 +- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/internal/cli/apps.go b/internal/cli/apps.go index c7e9b0740..6961b8c98 100644 --- a/internal/cli/apps.go +++ b/internal/cli/apps.go @@ -21,6 +21,7 @@ const ( appTypeRegularWeb = "regular_web" appTypeNonInteractive = "non_interactive" appDefaultURL = "http://localhost:3000" + defaultPageSize = 50 ) var ( @@ -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", } ) @@ -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"}, @@ -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 } diff --git a/internal/display/apps.go b/internal/display/apps.go index db1056238..f44e10ca4 100644 --- a/internal/display/apps.go +++ b/internal/display/apps.go @@ -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)