diff --git a/pkg/cmd/attempt_list.go b/pkg/cmd/attempt_list.go index bfd224aa..d18c65a5 100644 --- a/pkg/cmd/attempt_list.go +++ b/pkg/cmd/attempt_list.go @@ -103,8 +103,7 @@ func (ac *attemptListCmd) runAttemptListCmd(cmd *cobra.Command, args []string) e } // Display pagination info - commandExample := "hookdeck gateway attempt list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/connection_list.go b/pkg/cmd/connection_list.go index 144c556e..3b331b8c 100644 --- a/pkg/cmd/connection_list.go +++ b/pkg/cmd/connection_list.go @@ -170,8 +170,7 @@ func (cc *connectionListCmd) runConnectionListCmd(cmd *cobra.Command, args []str } // Display pagination info - commandExample := "hookdeck gateway connection list" - printPaginationInfo(response.Pagination, commandExample) + printPaginationInfo(cmd, args, response.Pagination) return nil } diff --git a/pkg/cmd/destination_list.go b/pkg/cmd/destination_list.go index 3dd1e008..b5be9cc3 100644 --- a/pkg/cmd/destination_list.go +++ b/pkg/cmd/destination_list.go @@ -107,8 +107,7 @@ func (dc *destinationListCmd) runDestinationListCmd(cmd *cobra.Command, args []s } // Display pagination info - commandExample := "hookdeck gateway destination list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/event_list.go b/pkg/cmd/event_list.go index 62d159b5..4f0152d2 100644 --- a/pkg/cmd/event_list.go +++ b/pkg/cmd/event_list.go @@ -240,8 +240,7 @@ func (ec *eventListCmd) runEventListCmd(cmd *cobra.Command, args []string) error } // Display pagination info - commandExample := "hookdeck gateway event list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/issue_list.go b/pkg/cmd/issue_list.go index 97560c86..3adc6d14 100644 --- a/pkg/cmd/issue_list.go +++ b/pkg/cmd/issue_list.go @@ -118,8 +118,7 @@ func (ic *issueListCmd) runIssueListCmd(cmd *cobra.Command, args []string) error fmt.Println() } - commandExample := "hookdeck gateway issue list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/outpost_attempt.go b/pkg/cmd/outpost_attempt.go index d22d3fa2..c5f19f42 100644 --- a/pkg/cmd/outpost_attempt.go +++ b/pkg/cmd/outpost_attempt.go @@ -179,7 +179,7 @@ func (ac *outpostAttemptListCmd) run(cmd *cobra.Command, args []string) error { fmt.Println() } - printPaginationInfo(resp.Pagination, "hookdeck outpost attempt list") + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/outpost_event_list.go b/pkg/cmd/outpost_event_list.go index 7de8ac5e..51c64c24 100644 --- a/pkg/cmd/outpost_event_list.go +++ b/pkg/cmd/outpost_event_list.go @@ -119,7 +119,7 @@ func (ec *outpostEventListCmd) run(cmd *cobra.Command, args []string) error { fmt.Println() } - printPaginationInfo(resp.Pagination, "hookdeck outpost event list") + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/outpost_tenant_list.go b/pkg/cmd/outpost_tenant_list.go index 37f339da..5a14311e 100644 --- a/pkg/cmd/outpost_tenant_list.go +++ b/pkg/cmd/outpost_tenant_list.go @@ -99,7 +99,7 @@ func (tc *outpostTenantListCmd) runOutpostTenantListCmd(cmd *cobra.Command, args fmt.Println() } - printPaginationInfo(resp.Pagination, "hookdeck outpost tenant list") + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/pagination_output.go b/pkg/cmd/pagination_output.go index a1971cc0..48b87bb2 100644 --- a/pkg/cmd/pagination_output.go +++ b/pkg/cmd/pagination_output.go @@ -3,12 +3,17 @@ package cmd import ( "encoding/json" "fmt" + "regexp" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" ) // printPaginationInfo displays pagination cursors for text output -func printPaginationInfo(pagination hookdeck.PaginationResponse, commandExample string) { +func printPaginationInfo(cmd *cobra.Command, args []string, pagination hookdeck.PaginationResponse) { if pagination.Next == nil && pagination.Prev == nil { return } @@ -25,8 +30,52 @@ func printPaginationInfo(pagination hookdeck.PaginationResponse, commandExample if pagination.Next != nil { fmt.Println() fmt.Println("To get the next page:") - fmt.Printf(" %s --next %s\n", commandExample, *pagination.Next) + fmt.Printf(" %s\n", nextPageCommandLine(cmd, args, *pagination.Next)) + } +} + +// nextPageCommandLine rebuilds the command that was run, pointed at the next +// cursor, so that the next page is filtered and sorted like the current one. +// Of the global flags only those selecting the account are repeated; the others +// include credentials, which must not be echoed. +func nextPageCommandLine(cmd *cobra.Command, args []string, next string) string { + parts := []string{cmd.CommandPath()} + for _, arg := range args { + parts = append(parts, quoteShellArg(arg)) + } + + cmd.Flags().Visit(func(f *pflag.Flag) { + switch f.Name { + case "next", "prev": + return + case "profile", "hookdeck-config": + default: + if cmd.Root().PersistentFlags().Lookup(f.Name) != nil { + return + } + } + + switch { + case f.Value.Type() != "bool": + parts = append(parts, "--"+f.Name, quoteShellArg(f.Value.String())) + case f.Value.String() == "true": + parts = append(parts, "--"+f.Name) + default: + parts = append(parts, "--"+f.Name+"="+f.Value.String()) + } + }) + + return strings.Join(append(parts, "--next", quoteShellArg(next)), " ") +} + +var shellSafeArg = regexp.MustCompile(`^[A-Za-z0-9_@%+=:,./-]+$`) + +// quoteShellArg single-quotes a value that a POSIX shell would otherwise alter. +func quoteShellArg(value string) string { + if shellSafeArg.MatchString(value) { + return value } + return "'" + strings.ReplaceAll(value, "'", `'\''`) + "'" } // marshalListResponseWithPagination marshals a list response including pagination diff --git a/pkg/cmd/pagination_output_test.go b/pkg/cmd/pagination_output_test.go new file mode 100644 index 00000000..06486093 --- /dev/null +++ b/pkg/cmd/pagination_output_test.go @@ -0,0 +1,104 @@ +package cmd + +import ( + "io" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/hookdeck/hookdeck-cli/pkg/config" +) + +// nextPageCommand runs a gateway list command against a server that always has +// another page, and returns the command suggested for fetching it. +func nextPageCommand(t *testing.T, args ...string) string { + t.Helper() + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"models":[{"id":"id_1"}],"pagination":{"limit":1,"next":"cur_2"}}`)) + })) + t.Cleanup(server.Close) + + config.ResetAPIClientForTesting() + t.Cleanup(config.ResetAPIClientForTesting) + + old := Config + t.Cleanup(func() { Config = old }) + Config = config.Config{APIBaseURL: server.URL, LogLevel: "info"} + Config.Profile.APIKey = "sk_test_123456789012" + Config.Profile.ProjectId = "proj_1" + Config.Profile.ProjectType = config.ProjectTypeEventGateway + + root := &cobra.Command{Use: "hookdeck", SilenceUsage: true, SilenceErrors: true} + root.PersistentFlags().StringP("profile", "p", "", "") + root.PersistentFlags().String("api-key", "", "") + root.AddCommand(newGatewayCmd().cmd) + root.SetArgs(args) + + r, w, err := os.Pipe() + require.NoError(t, err) + stdout := os.Stdout + os.Stdout = w + execErr := root.Execute() + os.Stdout = stdout + require.NoError(t, w.Close()) + out, err := io.ReadAll(r) + require.NoError(t, err) + require.NoError(t, execErr) + + _, command, found := strings.Cut(string(out), "To get the next page:\n") + require.True(t, found, "no next page hint in %q", out) + return strings.TrimSpace(command) +} + +func TestNextPageCommandRepeatsTheCommandThatWasRun(t *testing.T) { + tests := []struct { + name string + args []string + want string + }{ + { + name: "keeps a required flag", + args: []string{"gateway", "attempt", "list", "--event-id", "evt_123"}, + want: "hookdeck gateway attempt list --event-id evt_123 --next cur_2", + }, + { + name: "keeps filters and replaces the previous cursor", + args: []string{"gateway", "event", "list", "--status", "FAILED", "--limit", "1", "--next", "cur_1"}, + want: "hookdeck gateway event list --limit 1 --status FAILED --next cur_2", + }, + { + name: "keeps a boolean flag", + args: []string{"gateway", "source", "list", "--disabled"}, + want: "hookdeck gateway source list --disabled --next cur_2", + }, + { + name: "quotes values for the shell", + args: []string{"gateway", "event", "list", "--body", `{"name":"it's"}`}, + want: `hookdeck gateway event list --body '{"name":"it'\''s"}' --next cur_2`, + }, + { + name: "keeps the profile but does not echo credentials", + args: []string{"gateway", "event", "list", "-p", "prod", "--api-key", "sk_test_other_123456"}, + want: "hookdeck gateway event list --profile prod --next cur_2", + }, + { + name: "keeps a positional argument", + args: []string{"gateway", "transformation", "executions", "list", "trs_123"}, + want: "hookdeck gateway transformation executions list trs_123 --next cur_2", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, nextPageCommand(t, tt.args...)) + }) + } +} diff --git a/pkg/cmd/request_list.go b/pkg/cmd/request_list.go index cd347880..9a51c70a 100644 --- a/pkg/cmd/request_list.go +++ b/pkg/cmd/request_list.go @@ -203,8 +203,7 @@ func (rc *requestListCmd) runRequestListCmd(cmd *cobra.Command, args []string) e } // Display pagination info - commandExample := "hookdeck gateway request list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/source_list.go b/pkg/cmd/source_list.go index a5af75ca..51e0da1d 100644 --- a/pkg/cmd/source_list.go +++ b/pkg/cmd/source_list.go @@ -105,8 +105,7 @@ func (sc *sourceListCmd) runSourceListCmd(cmd *cobra.Command, args []string) err } // Display pagination info - commandExample := "hookdeck gateway source list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/transformation_executions.go b/pkg/cmd/transformation_executions.go index 2965d12c..ae6b4ea0 100644 --- a/pkg/cmd/transformation_executions.go +++ b/pkg/cmd/transformation_executions.go @@ -131,8 +131,7 @@ func (tc *transformationExecutionsListCmd) run(cmd *cobra.Command, args []string } // Display pagination info - commandExample := "hookdeck gateway transformation executions list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil } diff --git a/pkg/cmd/transformation_list.go b/pkg/cmd/transformation_list.go index d591dcbd..af0a3f18 100644 --- a/pkg/cmd/transformation_list.go +++ b/pkg/cmd/transformation_list.go @@ -106,8 +106,7 @@ func (tc *transformationListCmd) runTransformationListCmd(cmd *cobra.Command, ar } // Display pagination info - commandExample := "hookdeck gateway transformation list" - printPaginationInfo(resp.Pagination, commandExample) + printPaginationInfo(cmd, args, resp.Pagination) return nil }