Skip to content

Commit

Permalink
refactor: Move source query parsing logic to listen package
Browse files Browse the repository at this point in the history
  • Loading branch information
alexluong committed Jul 10, 2024
1 parent 6720414 commit bcca2d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
16 changes: 2 additions & 14 deletions pkg/cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func newListenCmd() *listenCmd {

// listenCmd represents the listen command
func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {
var sourceQueryString, connectionQuery string
var sourceQuery, connectionQuery string
if len(args) > 1 {
sourceQueryString = args[1]
sourceQuery = args[1]
}
if len(args) > 2 {
connectionQuery = args[2]
Expand All @@ -112,18 +112,6 @@ func (lc *listenCmd) runListenCmd(cmd *cobra.Command, args []string) error {
url.Scheme = "http"
}

var sourceQuery []string
if sourceQueryString == "" {
sourceQuery = []string{}
} else {
sourceQuery = strings.Split(sourceQueryString, ",")
}

// TODO: remove once we can support better limit
if len(sourceQuery) > 10 {
return errors.New("max 10 sources supported")
}

return listen.Listen(url, sourceQuery, connectionQuery, listen.Flags{
NoWSS: lc.noWSS,
}, &Config)
Expand Down
24 changes: 23 additions & 1 deletion pkg/listen/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net/url"
"regexp"
"strings"

"github.com/hookdeck/hookdeck-cli/pkg/config"
"github.com/hookdeck/hookdeck-cli/pkg/login"
Expand All @@ -34,10 +35,15 @@ type Flags struct {
}

// listenCmd represents the listen command
func Listen(URL *url.URL, sourceAliases []string, connectionQuery string, flags Flags, config *config.Config) error {
func Listen(URL *url.URL, sourceQuery string, connectionQuery string, flags Flags, config *config.Config) error {
var err error
var guestURL string

sourceAliases, err := parseSourceQuery(sourceQuery)
if err != nil {
return err
}

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

if config.Profile.APIKey == "" {
Expand Down Expand Up @@ -100,6 +106,22 @@ func Listen(URL *url.URL, sourceAliases []string, connectionQuery string, flags
return nil
}

func parseSourceQuery(sourceQuery string) ([]string, error) {
var sourceAliases []string
if sourceQuery == "" {
sourceAliases = []string{}
} else {
sourceAliases = strings.Split(sourceQuery, ",")
}

// TODO: remove once we can support better limit
if len(sourceAliases) > 10 {
return []string{}, errors.New("max 10 sources supported")
}

return sourceAliases, nil
}

func isPath(value string) (bool, error) {
is_path, err := regexp.MatchString(`^(\/)+([/a-zA-Z0-9-_%\.\-\_\~\!\$\&\'\(\)\*\+\,\;\=\:\@]*)$`, value)
return is_path, err
Expand Down

0 comments on commit bcca2d4

Please sign in to comment.