@@ -12,14 +12,16 @@ import (
1212 "github.com/kernel/cli/pkg/util"
1313 "github.com/kernel/kernel-go-sdk"
1414 "github.com/kernel/kernel-go-sdk/option"
15+ "github.com/kernel/kernel-go-sdk/packages/pagination"
1516 "github.com/pterm/pterm"
17+ "github.com/samber/lo"
1618 "github.com/spf13/cobra"
1719)
1820
1921// ProfilesService defines the subset of the Kernel SDK profile client that we use.
2022type ProfilesService interface {
2123 Get (ctx context.Context , idOrName string , opts ... option.RequestOption ) (res * kernel.Profile , err error )
22- List (ctx context.Context , opts ... option.RequestOption ) (res * [] kernel.Profile , err error )
24+ List (ctx context.Context , query kernel. ProfileListParams , opts ... option.RequestOption ) (res * pagination. OffsetPagination [ kernel.Profile ] , err error )
2325 Delete (ctx context.Context , idOrName string , opts ... option.RequestOption ) (err error )
2426 New (ctx context.Context , body kernel.ProfileNewParams , opts ... option.RequestOption ) (res * kernel.Profile , err error )
2527 Download (ctx context.Context , idOrName string , opts ... option.RequestOption ) (res * http.Response , err error )
@@ -31,7 +33,10 @@ type ProfilesGetInput struct {
3133}
3234
3335type ProfilesListInput struct {
34- Output string
36+ Output string
37+ Page int
38+ PerPage int
39+ Query string
3540}
3641
3742type ProfilesCreateInput struct {
@@ -60,28 +65,56 @@ func (p ProfilesCmd) List(ctx context.Context, in ProfilesListInput) error {
6065 return fmt .Errorf ("unsupported --output value: use 'json'" )
6166 }
6267
68+ page := in .Page
69+ perPage := in .PerPage
70+ if page <= 0 {
71+ page = 1
72+ }
73+ if perPage <= 0 {
74+ perPage = 20
75+ }
76+
6377 if in .Output != "json" {
6478 pterm .Info .Println ("Fetching profiles..." )
6579 }
66- items , err := p .profiles .List (ctx )
80+
81+ params := kernel.ProfileListParams {}
82+ if in .Query != "" {
83+ params .Query = kernel .Opt (in .Query )
84+ }
85+ params .Limit = kernel .Opt (int64 (perPage + 1 ))
86+ params .Offset = kernel .Opt (int64 ((page - 1 ) * perPage ))
87+
88+ result , err := p .profiles .List (ctx , params )
6789 if err != nil {
6890 return util.CleanedUpSdkError {Err : err }
6991 }
7092
93+ var items []kernel.Profile
94+ if result != nil {
95+ items = result .Items
96+ }
97+
98+ hasMore := len (items ) > perPage
99+ if hasMore {
100+ items = items [:perPage ]
101+ }
102+ itemsThisPage := len (items )
103+
71104 if in .Output == "json" {
72- if items == nil || len (* items ) == 0 {
105+ if len (items ) == 0 {
73106 fmt .Println ("[]" )
74107 return nil
75108 }
76- return util .PrintPrettyJSONSlice (* items )
109+ return util .PrintPrettyJSONSlice (items )
77110 }
78111
79- if items == nil || len (* items ) == 0 {
112+ if len (items ) == 0 {
80113 pterm .Info .Println ("No profiles found" )
81114 return nil
82115 }
83116 rows := pterm.TableData {{"Profile ID" , "Name" , "Created At" , "Updated At" , "Last Used At" }}
84- for _ , prof := range * items {
117+ for _ , prof := range items {
85118 name := prof .Name
86119 if name == "" {
87120 name = "-"
@@ -95,6 +128,17 @@ func (p ProfilesCmd) List(ctx context.Context, in ProfilesListInput) error {
95128 })
96129 }
97130 PrintTableNoPad (rows , true )
131+
132+ pterm .Printf ("\n Page: %d Per-page: %d Items this page: %d Has more: %s\n " , page , perPage , itemsThisPage , lo .Ternary (hasMore , "yes" , "no" ))
133+ if hasMore {
134+ nextPage := page + 1
135+ nextCmd := fmt .Sprintf ("kernel profile list --page %d --per-page %d" , nextPage , perPage )
136+ if in .Query != "" {
137+ nextCmd += fmt .Sprintf (" --query \" %s\" " , in .Query )
138+ }
139+ pterm .Printf ("Next: %s\n " , nextCmd )
140+ }
141+
98142 return nil
99143}
100144
@@ -299,6 +343,9 @@ func init() {
299343 profilesCmd .AddCommand (profilesDownloadCmd )
300344
301345 profilesListCmd .Flags ().StringP ("output" , "o" , "" , "Output format: json for raw API response" )
346+ profilesListCmd .Flags ().Int ("per-page" , 20 , "Items per page (default 20)" )
347+ profilesListCmd .Flags ().Int ("page" , 1 , "Page number (1-based)" )
348+ profilesListCmd .Flags ().String ("query" , "" , "Search profiles by name or ID" )
302349 profilesGetCmd .Flags ().StringP ("output" , "o" , "" , "Output format: json for raw API response" )
303350 profilesCreateCmd .Flags ().StringP ("output" , "o" , "" , "Output format: json for raw API response" )
304351 profilesCreateCmd .Flags ().String ("name" , "" , "Optional unique profile name" )
@@ -310,9 +357,18 @@ func init() {
310357func runProfilesList (cmd * cobra.Command , args []string ) error {
311358 client := getKernelClient (cmd )
312359 output , _ := cmd .Flags ().GetString ("output" )
360+ perPage , _ := cmd .Flags ().GetInt ("per-page" )
361+ page , _ := cmd .Flags ().GetInt ("page" )
362+ query , _ := cmd .Flags ().GetString ("query" )
363+
313364 svc := client .Profiles
314365 p := ProfilesCmd {profiles : & svc }
315- return p .List (cmd .Context (), ProfilesListInput {Output : output })
366+ return p .List (cmd .Context (), ProfilesListInput {
367+ Output : output ,
368+ Page : page ,
369+ PerPage : perPage ,
370+ Query : query ,
371+ })
316372}
317373
318374func runProfilesGet (cmd * cobra.Command , args []string ) error {
0 commit comments