Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions pkg/cmd/attempt_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cmd/connection_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cmd/destination_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cmd/event_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cmd/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/outpost_attempt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/outpost_event_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/cmd/outpost_tenant_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
53 changes: 51 additions & 2 deletions pkg/cmd/pagination_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
104 changes: 104 additions & 0 deletions pkg/cmd/pagination_output_test.go
Original file line number Diff line number Diff line change
@@ -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...))
})
}
}
3 changes: 1 addition & 2 deletions pkg/cmd/request_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cmd/source_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions pkg/cmd/transformation_executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/transformation_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}