Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The default page size 250 is used here as a magic number. It's better to define this as a constant at the package level, for example const defaultListPageSize = 250. This improves code clarity and maintainability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — extracted to defaultListPageSize constant.

}

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This new implementation of ListWorkflows changes the behavior of the --limit flag in a breaking way. Previously, it limited the total number of results. Now, it controls the page size for auto-pagination, and there is no longer a way to limit the total number of returned workflows. This can be surprising to users and may cause performance issues if they unintentionally fetch a very large number of workflows.

Consider re-introducing a way to limit the total number of results, perhaps by making --limit a total limit again and adding a new --page-size flag for when pagination is desired.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 --limit 100 default silently hid 210 of 310 workflows on prod. Nobody was using --limit to deliberately cap results; it was just a page size that happened to truncate output.

This is an internal CLI (n8nctl), so there's no external compatibility concern. Adding a separate --page-size flag would be YAGNI.


// 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)
Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The help text (0 = auto-paginate all) is a bit misleading. With the new implementation, auto-pagination of all results happens for any value of --limit, with the value determining the page size. The special case for 0 is that it uses a default page size (250). A clearer description would improve user understanding.

Suggested change
cmd.Flags().IntVar(&limit, "limit", 0, "Maximum number of workflows per page (0 = auto-paginate all)")
cmd.Flags().IntVar(&limit, "limit", 0, "Maximum number of workflows per page; all pages are fetched (0 = default size)")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — updated to Page size per API request (0 = default).

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")

Expand Down
Loading