@@ -5,10 +5,12 @@ import (
55 "encoding/json"
66 "fmt"
77 "os"
8+ "strings"
89
910 "github.com/spf13/cobra"
1011
1112 "github.com/hookdeck/hookdeck-cli/pkg/ansi"
13+ "github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
1214 "github.com/hookdeck/hookdeck-cli/pkg/validators"
1315)
1416
@@ -22,14 +24,19 @@ func newConnectionGetCmd() *connectionGetCmd {
2224 cc := & connectionGetCmd {}
2325
2426 cc .cmd = & cobra.Command {
25- Use : "get <connection-id>" ,
27+ Use : "get <connection-id-or-name >" ,
2628 Args : validators .ExactArgs (1 ),
2729 Short : "Get connection details" ,
2830 Long : `Get detailed information about a specific connection.
2931
32+ You can specify either a connection ID or name.
33+
3034Examples:
31- # Get connection details
32- hookdeck connection get conn_abc123` ,
35+ # Get connection by ID
36+ hookdeck connection get conn_abc123
37+
38+ # Get connection by name
39+ hookdeck connection get my-connection` ,
3340 RunE : cc .runConnectionGetCmd ,
3441 }
3542
@@ -43,14 +50,20 @@ func (cc *connectionGetCmd) runConnectionGetCmd(cmd *cobra.Command, args []strin
4350 return err
4451 }
4552
46- connectionID := args [0 ]
47- client := Config .GetAPIClient ()
53+ connectionIDOrName := args [0 ]
54+ apiClient := Config .GetAPIClient ()
4855 ctx := context .Background ()
4956
57+ // Resolve connection ID from name or ID
58+ connectionID , err := resolveConnectionID (ctx , apiClient , connectionIDOrName )
59+ if err != nil {
60+ return err
61+ }
62+
5063 // Get connection by ID
51- conn , err := client .GetConnection (ctx , connectionID )
64+ conn , err := apiClient .GetConnection (ctx , connectionID )
5265 if err != nil {
53- return fmt . Errorf ( "failed to get connection: %w" , err )
66+ return formatConnectionError ( err , connectionIDOrName )
5467 }
5568
5669 if cc .output == "json" {
@@ -141,3 +154,62 @@ func (cc *connectionGetCmd) runConnectionGetCmd(cmd *cobra.Command, args []strin
141154
142155 return nil
143156}
157+
158+ // resolveConnectionID accepts both connection names and IDs
159+ // Try as ID first (if it starts with conn_ or web_), then lookup by name
160+ func resolveConnectionID (ctx context.Context , client * hookdeck.Client , nameOrID string ) (string , error ) {
161+ // If it looks like a connection ID, try it directly
162+ if strings .HasPrefix (nameOrID , "conn_" ) || strings .HasPrefix (nameOrID , "web_" ) {
163+ // Try to get it to verify it exists
164+ _ , err := client .GetConnection (ctx , nameOrID )
165+ if err == nil {
166+ return nameOrID , nil
167+ }
168+ // If we get a 404, fall through to name lookup
169+ // For other errors, format and return the error
170+ errMsg := strings .ToLower (err .Error ())
171+ if ! strings .Contains (errMsg , "404" ) && ! strings .Contains (errMsg , "not found" ) {
172+ return "" , err
173+ }
174+ // 404 on ID lookup - fall through to try name lookup
175+ }
176+
177+ // Try to find by name
178+ params := map [string ]string {
179+ "name" : nameOrID ,
180+ }
181+
182+ result , err := client .ListConnections (ctx , params )
183+ if err != nil {
184+ return "" , fmt .Errorf ("failed to lookup connection by name '%s': %w" , nameOrID , err )
185+ }
186+
187+ if result .Pagination .Limit == 0 || len (result .Models ) == 0 {
188+ return "" , fmt .Errorf ("connection not found: '%s'\n \n Please check the connection name or ID and try again" , nameOrID )
189+ }
190+
191+ if len (result .Models ) > 1 {
192+ return "" , fmt .Errorf ("multiple connections found with name '%s', please use the connection ID instead" , nameOrID )
193+ }
194+
195+ return result .Models [0 ].ID , nil
196+ }
197+
198+ // formatConnectionError provides user-friendly error messages for connection get failures
199+ func formatConnectionError (err error , identifier string ) error {
200+ errMsg := err .Error ()
201+
202+ // Check for 404/not found errors (case-insensitive)
203+ errMsgLower := strings .ToLower (errMsg )
204+ if strings .Contains (errMsgLower , "404" ) || strings .Contains (errMsgLower , "not found" ) {
205+ return fmt .Errorf ("connection not found: '%s'\n \n Please check the connection name or ID and try again" , identifier )
206+ }
207+
208+ // Check for network/timeout errors
209+ if strings .Contains (errMsg , "timeout" ) || strings .Contains (errMsg , "connection refused" ) {
210+ return fmt .Errorf ("failed to connect to Hookdeck API: %w\n \n Please check your network connection and try again" , err )
211+ }
212+
213+ // Default to the original error with some context
214+ return fmt .Errorf ("failed to get connection '%s': %w" , identifier , err )
215+ }
0 commit comments