Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix(cmd): url parse bug #520

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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