Skip to content

Commit

Permalink
bugfix(cmd): url parse bug (#520)
Browse files Browse the repository at this point in the history
* bugfix(cmd): url parse bug
  • Loading branch information
dakimura authored Oct 14, 2021
1 parent 7c523b8 commit 00eee5f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
15 changes: 11 additions & 4 deletions cmd/connect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package connect

import (
"errors"
"fmt"
"strings"

"github.com/alpacahq/marketstore/v4/frontend/client"

Expand Down Expand Up @@ -88,16 +90,21 @@ func executeConnect(cmd *cobra.Command, args []string) error {

// Attempt remote mode.
if len(url) != 0 {
// TODO: validate url using go core packages.
splits := strings.Split(url, ":")
if len(splits) != 2 {
return fmt.Errorf("incorrect URL, need \"hostname:port\", have: %s\n", url)
}
// build url.
url = "http://" + url

// Attempt connection to remote host.
rpcClient, err := client.NewClient(url)
if err != nil {
return err
}

conn, err = session.NewRemoteAPIClient(url, rpcClient)
if err != nil {
return err
}
conn = session.NewRemoteAPIClient(url, rpcClient)
}

if varCompOff {
Expand Down
14 changes: 2 additions & 12 deletions cmd/connect/session/remote_api_client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package session

import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/alpacahq/marketstore/v4/frontend"
Expand All @@ -13,16 +11,8 @@ import (
)

// NewRemoteAPIClient generates a new client struct.
func NewRemoteAPIClient(url string, client RPCClient) (rc *RemoteAPIClient, err error) {
// TODO: validate url using go core packages.
splits := strings.Split(url, ":")
if len(splits) != 2 {
msg := fmt.Sprintf("incorrect URL, need \"hostname:port\", have: %s\n", url)
return nil, errors.New(msg)
}
// build url.
url = "http://" + url
return &RemoteAPIClient{url: url, rpcClient: client}, nil
func NewRemoteAPIClient(url string, client RPCClient) *RemoteAPIClient {
return &RemoteAPIClient{url: url, rpcClient: client}
}

// RemoteAPIClient represents an agent that manages a database
Expand Down
6 changes: 2 additions & 4 deletions cmd/connect/session/remote_api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/alpacahq/marketstore/v4/cmd/connect/session"
"github.com/alpacahq/marketstore/v4/frontend"
"github.com/alpacahq/marketstore/v4/utils/io"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -66,12 +65,11 @@ func TestRemoteAPIClient_GetBucketInfo(t *testing.T) {
t.Parallel()

// --- given ---
rc, err := session.NewRemoteAPIClient("exampleurl:1234", tt.rpcClient)
assert.Nil(t, err)
rc := session.NewRemoteAPIClient("exampleurl:1234", tt.rpcClient)

// --- when ---
responses := &frontend.MultiGetInfoResponse{}
err = rc.GetBucketInfo(tt.reqs, responses)
err := rc.GetBucketInfo(tt.reqs, responses)

// --- then ---
if (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 00eee5f

Please sign in to comment.