@@ -2,93 +2,114 @@ package listen
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "strings"
87
9- "github.com/AlecAivazis/survey/v2"
108 "github.com/gosimple/slug"
119 hookdecksdk "github.com/hookdeck/hookdeck-go-sdk"
1210 hookdeckclient "github.com/hookdeck/hookdeck-go-sdk/client"
11+ log "github.com/sirupsen/logrus"
1312)
1413
15- func getConnections (client * hookdeckclient.Client , source * hookdecksdk.Source , connectionQuery string ) ([]* hookdecksdk.Connection , error ) {
16- // TODO: Filter connections using connectionQuery
17- var connections []* hookdecksdk.Connection
18- connectionList , err := client .Connection .List (context .Background (), & hookdecksdk.ConnectionListRequest {
19- SourceId : & source .Id ,
14+ func getConnections (client * hookdeckclient.Client , sources []* hookdecksdk.Source , connectionFilterString string , isMultiSource bool , cliPath string ) ([]* hookdecksdk.Connection , error ) {
15+ sourceIDs := []* string {}
16+
17+ for _ , source := range sources {
18+ sourceIDs = append (sourceIDs , & source .Id )
19+ }
20+
21+ connectionQuery , err := client .Connection .List (context .Background (), & hookdecksdk.ConnectionListRequest {
22+ SourceId : sourceIDs ,
2023 })
2124 if err != nil {
22- return nil , err
25+ return [] * hookdecksdk. Connection {} , err
2326 }
24- connections = connectionList .Models
2527
26- var filteredConnections []* hookdecksdk.Connection
28+ connections , err := filterConnections (connectionQuery .Models , connectionFilterString )
29+ if err != nil {
30+ return []* hookdecksdk.Connection {}, err
31+ }
32+
33+ connections , err = ensureConnections (client , connections , sources , isMultiSource , connectionFilterString , cliPath )
34+ if err != nil {
35+ return []* hookdecksdk.Connection {}, err
36+ }
37+
38+ return connections , nil
39+ }
40+
41+ // 1. Filter to only include CLI destination
42+ // 2. Apply connectionFilterString
43+ func filterConnections (connections []* hookdecksdk.Connection , connectionFilterString string ) ([]* hookdecksdk.Connection , error ) {
44+ // 1. Filter to only include CLI destination
45+ var cliDestinationConnections []* hookdecksdk.Connection
2746 for _ , connection := range connections {
2847 if connection .Destination .CliPath != nil && * connection .Destination .CliPath != "" {
29- filteredConnections = append (filteredConnections , connection )
48+ cliDestinationConnections = append (cliDestinationConnections , connection )
3049 }
3150 }
32- connections = filteredConnections
3351
34- if connectionQuery != "" {
35- is_path , err := isPath (connectionQuery )
36- if err != nil {
37- return connections , err
38- }
39- var filteredConnections []* hookdecksdk.Connection
40- for _ , connection := range connections {
41- if (is_path && connection .Destination .CliPath != nil && strings .Contains (* connection .Destination .CliPath , connectionQuery )) || (connection .Name != nil && * connection .Name == connectionQuery ) {
42- filteredConnections = append (filteredConnections , connection )
43- }
44- }
45- connections = filteredConnections
52+ if connectionFilterString == "" {
53+ return cliDestinationConnections , nil
4654 }
4755
48- if len (connections ) == 0 {
49- answers := struct {
50- Label string `survey:"label"`
51- Path string `survey:"path"`
52- }{}
53- var qs = []* survey.Question {
54- {
55- Name : "path" ,
56- Prompt : & survey.Input {Message : "What path should the events be forwarded to (ie: /webhooks)?" },
57- Validate : func (val interface {}) error {
58- str , ok := val .(string )
59- is_path , err := isPath (str )
60- if ! ok || ! is_path || err != nil {
61- return errors .New ("invalid path" )
62- }
63- return nil
64- },
65- },
66- {
67- Name : "label" ,
68- Prompt : & survey.Input {Message : "What's your connection label (ie: My API)?" },
69- Validate : survey .Required ,
70- },
56+ // 2. Apply connectionFilterString
57+ isPath , err := isPath (connectionFilterString )
58+ if err != nil {
59+ return connections , err
60+ }
61+ var filteredConnections []* hookdecksdk.Connection
62+ for _ , connection := range cliDestinationConnections {
63+ if (isPath && connection .Destination .CliPath != nil && strings .Contains (* connection .Destination .CliPath , connectionFilterString )) || (connection .Name != nil && * connection .Name == connectionFilterString ) {
64+ filteredConnections = append (filteredConnections , connection )
7165 }
66+ }
7267
73- err := survey .Ask (qs , & answers )
74- if err != nil {
75- fmt .Println (err .Error ())
76- return connections , err
77- }
78- alias := slug .Make (answers .Label )
79- connection , err := client .Connection .Create (context .Background (), & hookdecksdk.ConnectionCreateRequest {
80- Name : hookdecksdk .OptionalOrNull (& alias ),
81- SourceId : hookdecksdk .OptionalOrNull (& source .Id ),
82- Destination : hookdecksdk .OptionalOrNull (& hookdecksdk.ConnectionCreateRequestDestination {
83- Name : alias ,
84- CliPath : & answers .Path ,
85- }),
86- })
87- if err != nil {
88- return connections , err
89- }
90- connections = append (connections , connection )
68+ return filteredConnections , nil
69+ }
70+
71+ // When users want to listen to a single source but there is no connection for that source,
72+ // we can help user set up a new connection for it.
73+ func ensureConnections (client * hookdeckclient.Client , connections []* hookdecksdk.Connection , sources []* hookdecksdk.Source , isMultiSource bool , connectionFilterString string , cliPath string ) ([]* hookdecksdk.Connection , error ) {
74+ if len (connections ) > 0 || isMultiSource {
75+ log .Debug (fmt .Sprintf ("Connection exists for Source \" %s\" , Connection \" %s\" , and CLI path \" %s\" " , sources [0 ].Name , connectionFilterString , cliPath ))
76+
77+ return connections , nil
78+ }
79+
80+ log .Debug (fmt .Sprintf ("No connection found. Creating a connection for Source \" %s\" , Connection \" %s\" , and CLI path \" %s\" " , sources [0 ].Name , connectionFilterString , cliPath ))
81+
82+ connectionDetails := struct {
83+ Label string `survey:"label"`
84+ Path string `survey:"path"`
85+ }{}
86+
87+ if len (connectionFilterString ) == 0 {
88+ connectionDetails .Label = "cli"
89+ } else {
90+ connectionDetails .Label = connectionFilterString
91+ }
92+
93+ if len (cliPath ) == 0 {
94+ connectionDetails .Path = "/"
95+ } else {
96+ connectionDetails .Path = cliPath
97+ }
98+
99+ alias := slug .Make (connectionDetails .Label )
100+
101+ connection , err := client .Connection .Create (context .Background (), & hookdecksdk.ConnectionCreateRequest {
102+ Name : hookdecksdk .OptionalOrNull (& alias ),
103+ SourceId : hookdecksdk .OptionalOrNull (& sources [0 ].Id ),
104+ Destination : hookdecksdk .OptionalOrNull (& hookdecksdk.ConnectionCreateRequestDestination {
105+ Name : alias ,
106+ CliPath : & connectionDetails .Path ,
107+ }),
108+ })
109+ if err != nil {
110+ return connections , err
91111 }
112+ connections = append (connections , connection )
92113
93114 return connections , nil
94115}
0 commit comments