Skip to content

Commit 42da7d8

Browse files
author
Paola Nicosia
committed
feat: add page option in paginated command
1 parent 3dfa36b commit 42da7d8

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

internal/clioptions/clioptions.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type CLIOptions struct {
6161
UserEmails []string
6262
UserIDs []string
6363

64+
Page int
65+
6466
ServiceAccountID string
6567

6668
BasicClientID string
@@ -253,6 +255,12 @@ func (o *CLIOptions) AddPublicFlag(flags *pflag.FlagSet) (flagName string) {
253255
return
254256
}
255257

258+
func (o *CLIOptions) AddPageFlag(flags *pflag.FlagSet) (flagName string) {
259+
flagName = "page"
260+
flags.IntVar(&o.Page, flagName, 1, "specify the page to fetch")
261+
return
262+
}
263+
256264
func (o *CLIOptions) AddMarketplaceItemObjectIDFlag(flags *pflag.FlagSet) (flagName string) {
257265
flagName = "object-id"
258266
flags.StringVar(&o.MarketplaceItemObjectID, flagName, "", "The _id of the Marketplace item")

internal/cmd/catalog/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ const (
3030
3131
This command lists the Catalog items of a company. It works with Mia-Platform Console v14.0.0 or later.
3232
33+
Results are paginated. By default, only the first page is shown.
34+
3335
you can also specify the following flags:
3436
- --public - if this flag is set, the command fetches not only the items from the requested company, but also the public Catalog items from other companies.
37+
- -- page - specify the page to fetch, default is 1
3538
`
3639
listCmdUse = "list --company-id company-id"
3740
)
@@ -46,6 +49,7 @@ func ListCmd(options *clioptions.CLIOptions) *cobra.Command {
4649
}
4750

4851
options.AddPublicFlag(cmd.Flags())
52+
options.AddPageFlag(cmd.Flags())
4953

5054
return cmd
5155
}
@@ -65,6 +69,7 @@ func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []
6569
marketplaceItemsOptions := commonMarketplace.GetMarketplaceItemsOptions{
6670
CompanyID: restConfig.CompanyID,
6771
Public: options.MarketplaceFetchPublicItems,
72+
Page: options.Page,
6873
}
6974

7075
err = commonMarketplace.PrintMarketplaceItems(cmd.Context(), apiClient, marketplaceItemsOptions, options.Printer(), listMarketplaceEndpoint)

internal/cmd/catalog/list_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func privateAndPublicMarketplaceHandler(t *testing.T) http.HandlerFunc {
157157
return func(w http.ResponseWriter, r *http.Request) {
158158
if strings.EqualFold(r.URL.Path, "/api/marketplace/") &&
159159
r.Method == http.MethodGet &&
160+
r.URL.Query().Get("page") == "1" &&
160161
r.URL.Query().Get("includeTenantId") == "my-company" {
161162
_, err := w.Write([]byte(marketplaceItemsBodyContent(t)))
162163
require.NoError(t, err)
@@ -172,6 +173,7 @@ func privateCompanyMarketplaceHandler(t *testing.T) http.HandlerFunc {
172173
return func(w http.ResponseWriter, r *http.Request) {
173174
if strings.EqualFold(r.URL.Path, "/api/marketplace/") &&
174175
r.Method == http.MethodGet &&
176+
r.URL.Query().Get("page") == "1" &&
175177
r.URL.Query().Get("tenantId") == "my-company" {
176178
_, err := w.Write([]byte(marketplacePrivateCompanyBodyContent(t)))
177179
require.NoError(t, err)

internal/cmd/common/marketplace/list.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package marketplace
1818
import (
1919
"context"
2020
"fmt"
21+
"strconv"
2122

2223
"github.com/mia-platform/miactl/internal/client"
2324
"github.com/mia-platform/miactl/internal/printer"
@@ -28,6 +29,7 @@ import (
2829
type GetMarketplaceItemsOptions struct {
2930
CompanyID string
3031
Public bool
32+
Page int
3133
}
3234

3335
func PrintMarketplaceItems(context context.Context, client *client.APIClient, options GetMarketplaceItemsOptions, p printer.IPrinter, endpoint string) error {
@@ -89,6 +91,15 @@ func buildRequest(client *client.APIClient, options GetMarketplaceItemsOptions,
8991
request.SetParam("tenantId", options.CompanyID)
9092
}
9193

94+
// marketplace command API does not support pagination
95+
if endpoint == "/api/marketplace/" {
96+
if options.Page <= 0 {
97+
request.SetParam("page", "1")
98+
} else {
99+
request.SetParam("page", strconv.Itoa(options.Page))
100+
}
101+
}
102+
92103
return request
93104
}
94105

internal/cmd/item-type-definition/list.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
type GetItdsOptions struct {
3232
CompanyID string
3333
Public bool
34+
Page int
3435
}
3536

3637
const (
@@ -39,8 +40,11 @@ const (
3940
4041
This command lists the Item Type Definitions of a company. It works with Mia-Platform Console v14.1.0 or later.
4142
43+
Results are paginated. By default, only the first page is shown.
44+
4245
you can also specify the following flags:
4346
- --public - if this flag is set, the command fetches not only the items from the requested company, but also the public Catalog items from other companies.
47+
- --page - specify the page to fetch, default is 1
4448
`
4549
listCmdUse = "list --company-id company-id"
4650
)
@@ -54,6 +58,7 @@ func ListCmd(options *clioptions.CLIOptions) *cobra.Command {
5458
}
5559

5660
options.AddPublicFlag(cmd.Flags())
61+
options.AddPageFlag(cmd.Flags())
5762

5863
return cmd
5964
}
@@ -70,12 +75,13 @@ func runListCmd(options *clioptions.CLIOptions) func(cmd *cobra.Command, args []
7075
return itd.ErrUnsupportedCompanyVersion
7176
}
7277

73-
marketplaceItemsOptions := GetItdsOptions{
78+
listItemsOptions := GetItdsOptions{
7479
CompanyID: restConfig.CompanyID,
7580
Public: options.MarketplaceFetchPublicItems,
81+
Page: options.Page,
7682
}
7783

78-
err = PrintItds(cmd.Context(), apiClient, marketplaceItemsOptions, options.Printer(), listItdEndpoint)
84+
err = PrintItds(cmd.Context(), apiClient, listItemsOptions, options.Printer(), listItdEndpoint)
7985
cobra.CheckErr(err)
8086

8187
return nil
@@ -119,12 +125,12 @@ func fetchItds(ctx context.Context, client *client.APIClient, options GetItdsOpt
119125
return nil, err
120126
}
121127

122-
marketplaceItems, err := parseResponse(resp)
128+
listItems, err := parseResponse(resp)
123129
if err != nil {
124130
return nil, err
125131
}
126132

127-
return marketplaceItems, nil
133+
return listItems, nil
128134
}
129135

130136
func validateOptions(options GetItdsOptions) error {
@@ -146,6 +152,12 @@ func buildRequest(client *client.APIClient, options GetItdsOptions, endpoint str
146152
request.SetParam("visibility", options.CompanyID)
147153
}
148154

155+
if options.Page <= 0 {
156+
request.SetParam("page", "1")
157+
} else {
158+
request.SetParam("page", strconv.Itoa(options.Page))
159+
}
160+
149161
return request
150162
}
151163

@@ -162,10 +174,10 @@ func executeRequest(ctx context.Context, request *client.Request) (*client.Respo
162174
}
163175

164176
func parseResponse(resp *client.Response) ([]*itd.ItemTypeDefinition, error) {
165-
marketplaceItems := make([]*itd.ItemTypeDefinition, 0)
166-
if err := resp.ParseResponse(&marketplaceItems); err != nil {
177+
listItems := make([]*itd.ItemTypeDefinition, 0)
178+
if err := resp.ParseResponse(&listItems); err != nil {
167179
return nil, fmt.Errorf("error parsing response body: %w", err)
168180
}
169181

170-
return marketplaceItems, nil
182+
return listItems, nil
171183
}

internal/cmd/item-type-definition/list_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ func TestBuildMarketplaceItemsList(t *testing.T) {
7979
},
8080
},
8181
{
82-
name: "public and private item type definitions",
82+
name: "public and private item type definitions, page 2",
8383
options: GetItdsOptions{
8484
CompanyID: "my-company",
8585
Public: true,
86+
Page: 2,
8687
},
8788
responseHandler: publicAndPrivateItdsHandler(t),
8889
clientConfig: &client.Config{Transport: http.DefaultTransport},
@@ -146,6 +147,7 @@ func privateItdsHandler(t *testing.T) http.HandlerFunc {
146147
return func(w http.ResponseWriter, r *http.Request) {
147148
if strings.EqualFold(r.URL.Path, listItdEndpoint) &&
148149
r.Method == http.MethodGet &&
150+
r.URL.Query().Get("page") == "1" &&
149151
r.URL.Query().Get("visibility") == "my-company" {
150152
_, err := w.Write([]byte(privateItdsBodyContent(t)))
151153
require.NoError(t, err)
@@ -161,6 +163,7 @@ func publicAndPrivateItdsHandler(t *testing.T) http.HandlerFunc {
161163
return func(w http.ResponseWriter, r *http.Request) {
162164
if strings.EqualFold(r.URL.Path, listItdEndpoint) &&
163165
r.Method == http.MethodGet &&
166+
r.URL.Query().Get("page") == "2" &&
164167
r.URL.Query().Get("visibility") == "console,my-company" {
165168
_, err := w.Write([]byte(publicItdsBodyContent(t)))
166169
require.NoError(t, err)

0 commit comments

Comments
 (0)