@@ -3,6 +3,7 @@ package list
33import (
44 "context"
55 "fmt"
6+ "math"
67 "strings"
78
89 "github.com/spf13/cobra"
@@ -23,10 +24,13 @@ import (
2324type inputModel struct {
2425 * globalflags.GlobalFlagModel
2526 SortBy string
27+ Limit * int32
2628}
2729
2830const (
29- sortByFlag = "sort-by"
31+ sortByFlag = "sort-by"
32+ limitFlag = ""
33+ maxPageSize = int32 (100 )
3034)
3135
3236func NewCmd (params * params.CmdParams ) * cobra.Command {
@@ -76,6 +80,7 @@ var sortByFlagOptions = []string{"id", "createdAt", "updatedAt", "originUrl", "s
7680func configureFlags (cmd * cobra.Command ) {
7781 // same default as apiClient
7882 cmd .Flags ().Var (flags .EnumFlag (false , "createdAt" , sortByFlagOptions ... ), sortByFlag , fmt .Sprintf ("Sort entries by a specific field, one of %q" , sortByFlagOptions ))
83+ cmd .Flags ().Int64 (limitFlag , 0 , "Limit the output to the first n elements" )
7984}
8085
8186func parseInput (p * print.Printer , cmd * cobra.Command , _ []string ) (* inputModel , error ) {
@@ -84,6 +89,14 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
8489 return nil , & errors.ProjectIdError {}
8590 }
8691
92+ limit := flags .FlagToInt32Pointer (p , cmd , limitFlag )
93+ if limit != nil && * limit < 1 {
94+ return nil , & errors.FlagValidationError {
95+ Flag : limitFlag ,
96+ Details : "must be greater than 0" ,
97+ }
98+ }
99+
87100 model := inputModel {
88101 GlobalFlagModel : globalFlags ,
89102 SortBy : flags .FlagWithDefaultToStringValue (p , cmd , sortByFlag ),
@@ -93,10 +106,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
93106 return & model , nil
94107}
95108
96- func buildRequest (ctx context.Context , model * inputModel , apiClient * cdn.APIClient , nextPageID cdn.ListDistributionsResponseGetNextPageIdentifierAttributeType ) cdn.ApiListDistributionsRequest {
109+ func buildRequest (ctx context.Context , model * inputModel , apiClient * cdn.APIClient , nextPageID cdn.ListDistributionsResponseGetNextPageIdentifierAttributeType , pageLimit int32 ) cdn.ApiListDistributionsRequest {
97110 req := apiClient .ListDistributions (ctx , model .GlobalFlagModel .ProjectId )
98111 req = req .SortBy (model .SortBy )
99- req = req .PageSize (100 )
112+ req = req .PageSize (pageLimit )
100113 if nextPageID != nil {
101114 req = req .PageIdentifier (* nextPageID )
102115 }
@@ -135,17 +148,24 @@ func outputResult(p *print.Printer, outputFormat string, distributions []cdn.Dis
135148func fetchDistributions (ctx context.Context , model * inputModel , apiClient * cdn.APIClient ) ([]cdn.Distribution , error ) {
136149 var nextPageID cdn.ListDistributionsResponseGetNextPageIdentifierAttributeType
137150 var distributions []cdn.Distribution
151+ received := int32 (0 )
152+ limit := int32 (math .MaxInt32 )
153+ if model .Limit != nil {
154+ limit = utils .Min (limit , * model .Limit )
155+ }
138156 for {
139- request := buildRequest (ctx , model , apiClient , nextPageID )
157+ want := utils .Min (maxPageSize , limit - received )
158+ request := buildRequest (ctx , model , apiClient , nextPageID , want )
140159 response , err := request .Execute ()
141160 if err != nil {
142161 return nil , fmt .Errorf ("list distributions: %w" , err )
143162 }
144- nextPageID = response .NextPageIdentifier
145163 if response .Distributions != nil {
146164 distributions = append (distributions , * response .Distributions ... )
147165 }
148- if nextPageID == nil {
166+ nextPageID = response .NextPageIdentifier
167+ received += want
168+ if nextPageID == nil || received >= limit {
149169 break
150170 }
151171 }
0 commit comments