From 78f74d870d4a0abfb915cc0e5c5854fec54ef690 Mon Sep 17 00:00:00 2001 From: Daito AKIMURA Date: Thu, 14 Oct 2021 09:56:18 +0900 Subject: [PATCH 1/2] bugfix(cmd): url parse bug --- cmd/connect/main.go | 10 ++++++++++ cmd/connect/session/remote_api_client.go | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/connect/main.go b/cmd/connect/main.go index 7571fda7..bebc269c 100644 --- a/cmd/connect/main.go +++ b/cmd/connect/main.go @@ -2,6 +2,8 @@ package connect import ( "errors" + "fmt" + "strings" "github.com/alpacahq/marketstore/v4/frontend/client" @@ -88,6 +90,14 @@ 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 { diff --git a/cmd/connect/session/remote_api_client.go b/cmd/connect/session/remote_api_client.go index dee7a440..1ac20de2 100644 --- a/cmd/connect/session/remote_api_client.go +++ b/cmd/connect/session/remote_api_client.go @@ -1,10 +1,8 @@ package session import ( - "errors" "fmt" "os" - "strings" "time" "github.com/alpacahq/marketstore/v4/frontend" @@ -14,14 +12,6 @@ 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 } From c9f36a74a5e6d94f90803119805a167bb4de77f8 Mon Sep 17 00:00:00 2001 From: Daito AKIMURA Date: Thu, 14 Oct 2021 10:00:06 +0900 Subject: [PATCH 2/2] refactor(cmd): remove unnecessary error return --- cmd/connect/main.go | 5 +---- cmd/connect/session/remote_api_client.go | 4 ++-- cmd/connect/session/remote_api_client_test.go | 6 ++---- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/cmd/connect/main.go b/cmd/connect/main.go index bebc269c..23e45d76 100644 --- a/cmd/connect/main.go +++ b/cmd/connect/main.go @@ -104,10 +104,7 @@ func executeConnect(cmd *cobra.Command, args []string) error { return err } - conn, err = session.NewRemoteAPIClient(url, rpcClient) - if err != nil { - return err - } + conn = session.NewRemoteAPIClient(url, rpcClient) } if varCompOff { diff --git a/cmd/connect/session/remote_api_client.go b/cmd/connect/session/remote_api_client.go index 1ac20de2..106c4234 100644 --- a/cmd/connect/session/remote_api_client.go +++ b/cmd/connect/session/remote_api_client.go @@ -11,8 +11,8 @@ import ( ) // NewRemoteAPIClient generates a new client struct. -func NewRemoteAPIClient(url string, client RPCClient) (rc *RemoteAPIClient, err error) { - 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 diff --git a/cmd/connect/session/remote_api_client_test.go b/cmd/connect/session/remote_api_client_test.go index 243034c1..0c767d6c 100644 --- a/cmd/connect/session/remote_api_client_test.go +++ b/cmd/connect/session/remote_api_client_test.go @@ -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" @@ -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 {