-
Notifications
You must be signed in to change notification settings - Fork 0
fix: auto-paginate workflow list #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,8 +170,8 @@ func (c *Client) request(method, path string, body interface{}) ([]byte, error) | |
| return respBody, nil | ||
| } | ||
|
|
||
| // ListWorkflows returns all workflows | ||
| func (c *Client) ListWorkflows(opts ListWorkflowsOptions) (*ListResult[Workflow], error) { | ||
| // listWorkflowsPage fetches a single page of workflows. | ||
| func (c *Client) listWorkflowsPage(opts ListWorkflowsOptions) (*ListResult[Workflow], error) { | ||
| params := url.Values{} | ||
| if opts.Limit > 0 { | ||
| params.Set("limit", strconv.Itoa(opts.Limit)) | ||
|
|
@@ -213,6 +213,39 @@ func (c *Client) ListWorkflows(opts ListWorkflowsOptions) (*ListResult[Workflow] | |
| return &resp, nil | ||
| } | ||
|
|
||
| // ListWorkflows returns all workflows, auto-paginating through all pages. | ||
| // If opts.Cursor is set, only that single page is returned (manual pagination). | ||
| func (c *Client) ListWorkflows(opts ListWorkflowsOptions) (*ListResult[Workflow], error) { | ||
| // Manual pagination: caller provided a cursor, return single page | ||
| if opts.Cursor != "" { | ||
| return c.listWorkflowsPage(opts) | ||
| } | ||
|
|
||
| pageSize := opts.Limit | ||
| if pageSize <= 0 { | ||
| pageSize = 250 | ||
| } | ||
|
|
||
| var all []Workflow | ||
| pageOpts := opts | ||
| pageOpts.Limit = pageSize | ||
|
|
||
| for { | ||
| page, err := c.listWorkflowsPage(pageOpts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| all = append(all, page.Data...) | ||
|
|
||
| if page.NextCursor == "" || len(page.Data) == 0 { | ||
| break | ||
| } | ||
| pageOpts.Cursor = page.NextCursor | ||
| } | ||
|
|
||
| return &ListResult[Workflow]{Data: all}, nil | ||
| } | ||
|
Comment on lines
+220
to
+249
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new implementation of Consider re-introducing a way to limit the total number of results, perhaps by making
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the intentional behavior change — the whole point of this PR. The old This is an internal CLI ( |
||
|
|
||
| // GetWorkflow returns a workflow by ID | ||
| func (c *Client) GetWorkflow(id string) (*Workflow, error) { | ||
| respBody, err := c.request(http.MethodGet, "/workflows/"+url.PathEscape(id), nil) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -108,19 +108,15 @@ func newListCmd() *cobra.Command { | |||||
| fmt.Printf("%-18s %-6s %s\n", wf.ID, activeStr, wf.Name) | ||||||
| } | ||||||
|
|
||||||
| if result.NextCursor != "" { | ||||||
| fmt.Printf("\nMore results available. Use --cursor %s to continue.\n", result.NextCursor) | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| cmd.Flags().BoolVar(&active, "active", false, "Show only active workflows") | ||||||
| cmd.Flags().BoolVar(&inactive, "inactive", false, "Show only inactive workflows") | ||||||
| cmd.Flags().StringSliceVar(&tags, "tag", nil, "Filter by tag (can be repeated)") | ||||||
| cmd.Flags().IntVar(&limit, "limit", 100, "Maximum number of workflows to return") | ||||||
| cmd.Flags().StringVar(&cursor, "cursor", "", "Pagination cursor for next page") | ||||||
| cmd.Flags().IntVar(&limit, "limit", 0, "Maximum number of workflows per page (0 = auto-paginate all)") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The help text
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed — updated to |
||||||
| cmd.Flags().StringVar(&cursor, "cursor", "", "Pagination cursor (fetches single page only)") | ||||||
| cmd.Flags().StringVar(&projectID, "project", "", "Filter by project ID") | ||||||
| cmd.Flags().StringVar(&name, "name", "", "Filter by workflow name") | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default page size
250is used here as a magic number. It's better to define this as a constant at the package level, for exampleconst defaultListPageSize = 250. This improves code clarity and maintainability.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed — extracted to
defaultListPageSizeconstant.