diff --git a/README.md b/README.md index e812740..5bb24b6 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ hookdeck login Start a session to forward your events to an HTTP server. ```sh-session -hookdeck listen [--cli-path?] +hookdeck listen [--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. @@ -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 diff --git a/pkg/cmd/listen.go b/pkg/cmd/listen.go index 7513636..e09a924 100644 --- a/pkg/cmd/listen.go +++ b/pkg/cmd/listen.go @@ -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 { @@ -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") @@ -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() @@ -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) @@ -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) } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 6534bbc..83db790 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -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") diff --git a/pkg/listen/connection.go b/pkg/listen/connection.go index b9f42a4..c459ca4 100644 --- a/pkg/listen/connection.go +++ b/pkg/listen/connection.go @@ -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 { @@ -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 } @@ -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 @@ -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{ diff --git a/pkg/listen/listen.go b/pkg/listen/listen.go index edde953..20d808f 100644 --- a/pkg/listen/listen.go +++ b/pkg/listen/listen.go @@ -31,8 +31,8 @@ import ( ) type Flags struct { - NoWSS bool - CliPath string + NoWSS bool + Path string } // listenCmd represents the listen command @@ -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") } } @@ -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, "", 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), })