Skip to content

Commit cad1698

Browse files
authored
Merge pull request #97 from hookdeck/chore/cli-path-tweaks
Change --cli-path to --path and hide --{x}-base dev flags
2 parents 404679f + a962081 commit cad1698

5 files changed

Lines changed: 53 additions & 34 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ hookdeck login
105105
Start a session to forward your events to an HTTP server.
106106

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

111111
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
159159

160160
#### Changing the path events are forwarded to
161161

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

164164
```sh-session
165-
$ hookdeck listen 3000 shopify orders --cli-path /events/shopify/orders
165+
$ hookdeck listen 3000 shopify orders --path /events/shopify/orders
166166

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

pkg/cmd/listen.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ import (
2424

2525
"github.com/hookdeck/hookdeck-cli/pkg/listen"
2626
"github.com/spf13/cobra"
27+
"github.com/spf13/pflag"
2728
)
2829

2930
type listenCmd struct {
30-
cmd *cobra.Command
31-
noWSS bool
32-
cliPath string
31+
cmd *cobra.Command
32+
noWSS bool
33+
path string
34+
}
35+
36+
// Map --cli-path to --path
37+
func normalizeCliPathFlag(f *pflag.FlagSet, name string) pflag.NormalizedName {
38+
switch name {
39+
case "cli-path":
40+
name = "path"
41+
break
42+
}
43+
return pflag.NormalizedName(name)
3344
}
3445

3546
func newListenCmd() *listenCmd {
@@ -42,8 +53,8 @@ func newListenCmd() *listenCmd {
4253
4354
This command will create a new Hookdeck Source if it doesn't exist.
4455
45-
By default the Hookdeck Destination will be named "CLI", and the
46-
Destination CLI path will be "/". To set the CLI path, use the "--cli-path" flag.`,
56+
By default the Hookdeck Destination will be named "{source}-cli", and the
57+
Destination CLI path will be "/". To set the CLI path, use the "--path" flag.`,
4758
Args: func(cmd *cobra.Command, args []string) error {
4859
if len(args) < 1 {
4960
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
8394
}
8495
lc.cmd.Flags().BoolVar(&lc.noWSS, "no-wss", false, "Force unencrypted ws:// protocol instead of wss://")
8596
lc.cmd.Flags().MarkHidden("no-wss")
86-
lc.cmd.Flags().StringVar(&lc.cliPath, "cli-path", "", "Sets the server path of that locally running web server the events will be forwarded to")
97+
98+
lc.cmd.Flags().StringVar(&lc.path, "path", "", "Sets the path to which events are forwarded e.g., /webhooks or /api/stripe")
99+
100+
// --cli-path is an alias for
101+
lc.cmd.Flags().SetNormalizeFunc(normalizeCliPathFlag)
87102

88103
usage := lc.cmd.UsageTemplate()
89104

@@ -113,7 +128,7 @@ Examples:
113128
114129
Forward events to the path "/webhooks" on local server running on port %[1]d:
115130
116-
hookdeck listen %[1]d --cli-path /webhooks
131+
hookdeck listen %[1]d --path /webhooks
117132
`, 3000)
118133

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

150165
return listen.Listen(url, sourceQuery, connectionQuery, listen.Flags{
151-
NoWSS: lc.noWSS,
152-
CliPath: lc.cliPath,
166+
NoWSS: lc.noWSS,
167+
Path: lc.path,
153168
}, &Config)
154169
}

pkg/cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ func init() {
9898

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

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

pkg/listen/connection.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

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

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

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

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

7676
return connections, nil
7777
}
7878

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

8181
connectionDetails := struct {
8282
ConnectionName string
@@ -92,10 +92,10 @@ func ensureConnections(client *hookdeckclient.Client, connections []*hookdecksdk
9292
connectionDetails.ConnectionName = connectionFilterString
9393
}
9494

95-
if len(cliPath) == 0 {
95+
if len(path) == 0 {
9696
connectionDetails.Path = "/"
9797
} else {
98-
connectionDetails.Path = cliPath
98+
connectionDetails.Path = path
9999
}
100100

101101
connection, err := client.Connection.Create(context.Background(), &hookdecksdk.ConnectionCreateRequest{

pkg/listen/listen.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
)
3232

3333
type Flags struct {
34-
NoWSS bool
35-
CliPath string
34+
NoWSS bool
35+
Path string
3636
}
3737

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

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

50-
if flags.CliPath != "" {
50+
if flags.Path != "" {
5151
if isMultiSource {
5252
return errors.New("Can only set a CLI path when listening to a single source")
5353
}
5454

55-
flagIsPath, err := isPath(flags.CliPath)
55+
flagIsPath, err := isPath(flags.Path)
5656
if err != nil {
5757
return err
5858
}
5959
if !flagIsPath {
60-
return errors.New("The CLI path must be in a valid format")
60+
return errors.New("The path must be in a valid format")
6161
}
6262
}
6363

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

80-
connections, err := getConnections(sdkClient, sources, connectionFilterString, isMultiSource, flags.CliPath)
80+
connections, err := getConnections(sdkClient, sources, connectionFilterString, isMultiSource, flags.Path)
8181
if err != nil {
8282
return err
8383
}
8484

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

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

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

101-
path := flags.CliPath
101+
path := flags.Path
102102
_, err := sdkClient.Destination.Update(context.Background(), connections[0].Destination.Id, &hookdecksdk.DestinationUpdateRequest{
103103
CliPath: hookdecksdk.Optional(path),
104104
})

0 commit comments

Comments
 (0)