Skip to content

Commit

Permalink
Merge pull request #97 from hookdeck/chore/cli-path-tweaks
Browse files Browse the repository at this point in the history
Change --cli-path to --path and hide --{x}-base dev flags
  • Loading branch information
leggetter authored Aug 5, 2024
2 parents 404679f + a962081 commit cad1698
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ hookdeck login
Start a session to forward your events to an HTTP server.

```sh-session
hookdeck listen <port-or-URL> <source-alias?> <connection-query?> [--cli-path?]
hookdeck listen <port-or-URL> <source-alias?> <connection-query?> [--path?]
```

Hookdeck works by routing events received for a given `source` (i.e., Shopify, Github, etc.) to its defined `destination` by connecting them with a `connection` to a `destination`. The CLI allows you to receive events for any given connection and forward them to your localhost at the specified port or any valid URL.
Expand Down Expand Up @@ -159,10 +159,10 @@ Orders Service forwarding to /webhooks/shopify/orders

#### Changing the path events are forwarded to

The `--cli-path` flag allows you to change the path to which events are forwarded.
The `--path` flag sets the path to which events are forwarded.

```sh-session
$ hookdeck listen 3000 shopify orders --cli-path /events/shopify/orders
$ hookdeck listen 3000 shopify orders --path /events/shopify/orders

👉 Inspect and replay events: https://dashboard.hookdeck.com/cli/events

Expand Down
33 changes: 24 additions & 9 deletions pkg/cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ import (

"github.com/hookdeck/hookdeck-cli/pkg/listen"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type listenCmd struct {
cmd *cobra.Command
noWSS bool
cliPath string
cmd *cobra.Command
noWSS bool
path string
}

// Map --cli-path to --path
func normalizeCliPathFlag(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name {
case "cli-path":
name = "path"
break
}
return pflag.NormalizedName(name)
}

func newListenCmd() *listenCmd {
Expand All @@ -42,8 +53,8 @@ func newListenCmd() *listenCmd {
This command will create a new Hookdeck Source if it doesn't exist.
By default the Hookdeck Destination will be named "CLI", and the
Destination CLI path will be "/". To set the CLI path, use the "--cli-path" flag.`,
By default the Hookdeck Destination will be named "{source}-cli", and the
Destination CLI path will be "/". To set the CLI path, use the "--path" flag.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires a port or forwarding URL to forward the events to")
Expand Down Expand Up @@ -83,7 +94,11 @@ Destination CLI path will be "/". To set the CLI path, use the "--cli-path" flag
}
lc.cmd.Flags().BoolVar(&lc.noWSS, "no-wss", false, "Force unencrypted ws:// protocol instead of wss://")
lc.cmd.Flags().MarkHidden("no-wss")
lc.cmd.Flags().StringVar(&lc.cliPath, "cli-path", "", "Sets the server path of that locally running web server the events will be forwarded to")

lc.cmd.Flags().StringVar(&lc.path, "path", "", "Sets the path to which events are forwarded e.g., /webhooks or /api/stripe")

// --cli-path is an alias for
lc.cmd.Flags().SetNormalizeFunc(normalizeCliPathFlag)

usage := lc.cmd.UsageTemplate()

Expand Down Expand Up @@ -113,7 +128,7 @@ Examples:
Forward events to the path "/webhooks" on local server running on port %[1]d:
hookdeck listen %[1]d --cli-path /webhooks
hookdeck listen %[1]d --path /webhooks
`, 3000)

lc.cmd.SetUsageTemplate(usage)
Expand Down Expand Up @@ -148,7 +163,7 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {
}

return listen.Listen(url, sourceQuery, connectionQuery, listen.Flags{
NoWSS: lc.noWSS,
CliPath: lc.cliPath,
NoWSS: lc.noWSS,
Path: lc.path,
}, &Config)
}
4 changes: 4 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ func init() {

// Hidden configuration flags, useful for dev/debugging
rootCmd.PersistentFlags().StringVar(&Config.APIBaseURL, "api-base", "", fmt.Sprintf("Sets the API base URL (default \"%s\")", hookdeck.DefaultAPIBaseURL))
rootCmd.PersistentFlags().MarkHidden("api-base")
rootCmd.PersistentFlags().StringVar(&Config.DashboardBaseURL, "dashboard-base", "", fmt.Sprintf("Sets the web dashboard base URL (default \"%s\")", hookdeck.DefaultDashboardBaseURL))
rootCmd.PersistentFlags().MarkHidden("dashboard-base")
rootCmd.PersistentFlags().StringVar(&Config.ConsoleBaseURL, "console-base", "", fmt.Sprintf("Sets the web console base URL (default \"%s\")", hookdeck.DefaultConsoleBaseURL))
rootCmd.PersistentFlags().MarkHidden("console-base")
rootCmd.PersistentFlags().StringVar(&Config.WSBaseURL, "ws-base", "", fmt.Sprintf("Sets the Websocket base URL (default \"%s\")", hookdeck.DefaultWebsocektURL))
rootCmd.PersistentFlags().MarkHidden("ws-base")

rootCmd.Flags().BoolP("version", "v", false, "Get the version of the Hookdeck CLI")

Expand Down
14 changes: 7 additions & 7 deletions pkg/listen/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
log "github.com/sirupsen/logrus"
)

func getConnections(client *hookdeckclient.Client, sources []*hookdecksdk.Source, connectionFilterString string, isMultiSource bool, cliPath string) ([]*hookdecksdk.Connection, error) {
func getConnections(client *hookdeckclient.Client, sources []*hookdecksdk.Source, connectionFilterString string, isMultiSource bool, path string) ([]*hookdecksdk.Connection, error) {
sourceIDs := []*string{}

for _, source := range sources {
Expand All @@ -29,7 +29,7 @@ func getConnections(client *hookdeckclient.Client, sources []*hookdecksdk.Source
return []*hookdecksdk.Connection{}, err
}

connections, err = ensureConnections(client, connections, sources, isMultiSource, connectionFilterString, cliPath)
connections, err = ensureConnections(client, connections, sources, isMultiSource, connectionFilterString, path)
if err != nil {
return []*hookdecksdk.Connection{}, err
}
Expand Down Expand Up @@ -69,14 +69,14 @@ func filterConnections(connections []*hookdecksdk.Connection, connectionFilterSt

// When users want to listen to a single source but there is no connection for that source,
// we can help user set up a new connection for it.
func ensureConnections(client *hookdeckclient.Client, connections []*hookdecksdk.Connection, sources []*hookdecksdk.Source, isMultiSource bool, connectionFilterString string, cliPath string) ([]*hookdecksdk.Connection, error) {
func ensureConnections(client *hookdeckclient.Client, connections []*hookdecksdk.Connection, sources []*hookdecksdk.Source, isMultiSource bool, connectionFilterString string, path string) ([]*hookdecksdk.Connection, error) {
if len(connections) > 0 || isMultiSource {
log.Debug(fmt.Sprintf("Connection exists for Source \"%s\", Connection \"%s\", and CLI path \"%s\"", sources[0].Name, connectionFilterString, cliPath))
log.Debug(fmt.Sprintf("Connection exists for Source \"%s\", Connection \"%s\", and path \"%s\"", sources[0].Name, connectionFilterString, path))

return connections, nil
}

log.Debug(fmt.Sprintf("No connection found. Creating a connection for Source \"%s\", Connection \"%s\", and CLI path \"%s\"", sources[0].Name, connectionFilterString, cliPath))
log.Debug(fmt.Sprintf("No connection found. Creating a connection for Source \"%s\", Connection \"%s\", and path \"%s\"", sources[0].Name, connectionFilterString, path))

connectionDetails := struct {
ConnectionName string
Expand All @@ -92,10 +92,10 @@ func ensureConnections(client *hookdeckclient.Client, connections []*hookdecksdk
connectionDetails.ConnectionName = connectionFilterString
}

if len(cliPath) == 0 {
if len(path) == 0 {
connectionDetails.Path = "/"
} else {
connectionDetails.Path = cliPath
connectionDetails.Path = path
}

connection, err := client.Connection.Create(context.Background(), &hookdecksdk.ConnectionCreateRequest{
Expand Down
30 changes: 15 additions & 15 deletions pkg/listen/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
)

type Flags struct {
NoWSS bool
CliPath string
NoWSS bool
Path string
}

// listenCmd represents the listen command
Expand All @@ -47,17 +47,17 @@ func Listen(URL *url.URL, sourceQuery string, connectionFilterString string, fla

isMultiSource := len(sourceAliases) > 1 || (len(sourceAliases) == 1 && sourceAliases[0] == "*")

if flags.CliPath != "" {
if flags.Path != "" {
if isMultiSource {
return errors.New("Can only set a CLI path when listening to a single source")
}

flagIsPath, err := isPath(flags.CliPath)
flagIsPath, err := isPath(flags.Path)
if err != nil {
return err
}
if !flagIsPath {
return errors.New("The CLI path must be in a valid format")
return errors.New("The path must be in a valid format")
}
}

Expand All @@ -77,28 +77,28 @@ func Listen(URL *url.URL, sourceQuery string, connectionFilterString string, fla
return err
}

connections, err := getConnections(sdkClient, sources, connectionFilterString, isMultiSource, flags.CliPath)
connections, err := getConnections(sdkClient, sources, connectionFilterString, isMultiSource, flags.Path)
if err != nil {
return err
}

if len(flags.CliPath) != 0 && len(connections) > 1 {
return errors.New(fmt.Errorf(`Multiple CLI destinations found. Cannot set the CLI path on multiple destinations.
Specify a single destination to update the CLI path. For example, pass a connection name:
if len(flags.Path) != 0 && len(connections) > 1 {
return errors.New(fmt.Errorf(`Multiple CLI destinations found. Cannot set the path on multiple destinations.
Specify a single destination to update the path. For example, pass a connection name:
hookdeck listen %s %s %s --cli-path %s`, URL.String(), sources[0].Name, "connection-name", flags.CliPath).Error())
hookdeck listen %s %s %s --path %s`, URL.String(), sources[0].Name, "<connection>", flags.Path).Error())
}

// If the "cli-path" flag has been passed and the destination has a current cli path value but it's different, update destination path
if len(flags.CliPath) != 0 &&
// If the "--path" flag has been passed and the destination has a current cli path value but it's different, update destination path
if len(flags.Path) != 0 &&
len(connections) == 1 &&
*connections[0].Destination.CliPath != "" &&
*connections[0].Destination.CliPath != flags.CliPath {
*connections[0].Destination.CliPath != flags.Path {

updateMsg := fmt.Sprintf("Updating destination CLI path from \"%s\" to \"%s\"", *connections[0].Destination.CliPath, flags.CliPath)
updateMsg := fmt.Sprintf("Updating destination CLI path from \"%s\" to \"%s\"", *connections[0].Destination.CliPath, flags.Path)
log.Debug(updateMsg)

path := flags.CliPath
path := flags.Path
_, err := sdkClient.Destination.Update(context.Background(), connections[0].Destination.Id, &hookdecksdk.DestinationUpdateRequest{
CliPath: hookdecksdk.Optional(path),
})
Expand Down

0 comments on commit cad1698

Please sign in to comment.