Skip to content

Commit

Permalink
chore: move ValidateUrl to util pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Oct 19, 2023
1 parent d1404a6 commit ed8b611
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
25 changes: 2 additions & 23 deletions cmd/loadtest/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package loadtest
import (
"crypto/ecdsa"
_ "embed"
"errors"
"fmt"
"math/big"
"math/rand"
"net/url"
"sync"
"time"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/maticnetwork/polygon-cli/rpctypes"
"github.com/rs/zerolog/log"
"github.com/maticnetwork/polygon-cli/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -223,7 +221,7 @@ func checkFlags() error {
if ltp.RPCUrl == nil {
panic("RPC URL is empty")
}
if err := validateUrl(*ltp.RPCUrl); err != nil {
if err := util.ValidateUrl(*ltp.RPCUrl); err != nil {
return err
}

Expand All @@ -237,22 +235,3 @@ func checkFlags() error {

return nil
}

// validateUrl checks if a string URL can be parsed and if it has a valid scheme.
func validateUrl(input string) error {
url, err := url.Parse(input)
if err != nil {
log.Error().Err(err).Msg("Unable to parse url input error")
return err
}

if url.Scheme == "" {
return errors.New("the scheme has not been specified")
}
switch url.Scheme {
case "http", "https", "ws", "wss":
return nil
default:
return fmt.Errorf("the scheme '%s' is not supported", url.Scheme)
}
}
28 changes: 28 additions & 0 deletions util/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package util

import (
"errors"
"fmt"
"net/url"

"github.com/rs/zerolog/log"
)

// ValidateUrl checks if a string URL can be parsed and if it has a valid scheme.
func ValidateUrl(input string) error {
url, err := url.Parse(input)
if err != nil {
log.Error().Err(err).Msg("Unable to parse url input error")
return err
}

if url.Scheme == "" {
return errors.New("the scheme has not been specified")
}
switch url.Scheme {
case "http", "https", "ws", "wss":
return nil
default:
return fmt.Errorf("the scheme '%s' is not supported", url.Scheme)
}
}

0 comments on commit ed8b611

Please sign in to comment.