@@ -3,6 +3,7 @@ package list
33import (
44 "context"
55 "fmt"
6+ "math"
67
78 "github.com/spf13/cobra"
89 registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
@@ -20,7 +21,8 @@ import (
2021)
2122
2223const (
23- limitFlag = "limit"
24+ limitFlag = "limit"
25+ maxPageSize = 100
2426)
2527
2628type inputModel struct {
@@ -59,16 +61,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
5961 }
6062
6163 // Call API
62- req := buildRequest (ctx , model , apiClient )
63- resp , err := req .Execute ()
64+ artifactories , err := fetchArtifactories (ctx , model , apiClient )
6465 if err != nil {
65- return fmt .Errorf ("get artifactories: %w" , err )
66- }
67- artifactories := resp .Artifactories
68-
69- // Truncate output
70- if model .Limit != nil && len (artifactories ) > int (* model .Limit ) {
71- artifactories = artifactories [:* model .Limit ]
66+ return err
7267 }
7368
7469 projectLabel , err := projectname .GetProjectName (ctx , params .Printer , params .CliVersion , cmd )
@@ -112,8 +107,51 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
112107 return & model , nil
113108}
114109
115- func buildRequest (ctx context.Context , model * inputModel , apiClient * registry.APIClient ) registry.ApiListArtifactoriesRequest {
116- return apiClient .DefaultAPI .ListArtifactories (ctx , model .ProjectId , model .Region )
110+ func buildRequest (ctx context.Context , model * inputModel , apiClient * registry.APIClient , pageToken string , pageSize int32 ) registry.ApiListArtifactoriesRequest {
111+ req := apiClient .DefaultAPI .ListArtifactories (ctx , model .ProjectId , model .Region )
112+ if pageToken != "" {
113+ req = req .NextPageToken (pageToken )
114+ }
115+ if pageSize > 0 {
116+ req = req .PageSize (pageSize )
117+ }
118+ return req
119+ }
120+
121+ func fetchArtifactories (ctx context.Context , model * inputModel , apiClient * registry.APIClient ) ([]registry.Artifactory , error ) {
122+ var pageToken string
123+ artifactories := make ([]registry.Artifactory , 0 )
124+ received := int64 (0 )
125+ limit := int64 (math .MaxInt64 )
126+ if model .Limit != nil {
127+ limit = * model .Limit
128+ }
129+ for {
130+ want := min (int64 (maxPageSize ), limit - received )
131+ request := buildRequest (ctx , model , apiClient , pageToken , int32 (want ))
132+ response , err := request .Execute ()
133+ if err != nil {
134+ return nil , fmt .Errorf ("get artifactories: %w" , err )
135+ }
136+ if response == nil {
137+ return nil , fmt .Errorf ("get artifactories: empty response" )
138+ }
139+ if response .Artifactories != nil {
140+ artifactories = append (artifactories , response .Artifactories ... )
141+ }
142+ pageToken = ""
143+ if response .NextPageToken != nil {
144+ pageToken = * response .NextPageToken
145+ }
146+ received += want
147+ if pageToken == "" || received >= limit {
148+ break
149+ }
150+ }
151+ if model .Limit != nil && len (artifactories ) > int (* model .Limit ) {
152+ artifactories = artifactories [:* model .Limit ]
153+ }
154+ return artifactories , nil
117155}
118156
119157func outputResult (p * print.Printer , outputFormat , projectLabel string , artifactories []registry.Artifactory ) error {
0 commit comments