diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/go/src/cmd/query.go | 39 | ||||
-rw-r--r-- | client/go/src/cmd/query_test.go | 2 |
2 files changed, 13 insertions, 28 deletions
diff --git a/client/go/src/cmd/query.go b/client/go/src/cmd/query.go index 569ff3b664e..bf678b19f4d 100644 --- a/client/go/src/cmd/query.go +++ b/client/go/src/cmd/query.go @@ -9,9 +9,10 @@ import ( "errors" "github.com/spf13/cobra" "github.com/vespa-engine/vespa/utils" - "regexp" "strings" + "net/http" "net/url" + "time" ) func init() { @@ -35,26 +36,15 @@ var queryCmd = &cobra.Command{ } func query(arguments []string) { - var argBuilder strings.Builder; + url, _ := url.Parse(getTarget(queryContext).query + "/search/") + urlQuery := url.Query() for i := 0; i < len(arguments); i++ { - argument := arguments[i] - - if ! startsByParameter(argument) { // Default parameter - argument = "yql=" + argument - } - - argument = escapePayload(argument) - if argument == "" { - return - } - if i > 0 { - argBuilder.WriteString("&") - } - argBuilder.WriteString(argument) + key, value := splitArg(arguments[i]) + urlQuery.Set(key, value) } + url.RawQuery = urlQuery.Encode() - path := "/search/?" + argBuilder.String() - response := utils.HttpGet(getTarget(queryContext).query, path, "Container") + response := utils.HttpDo(&http.Request{URL: url,}, time.Second * 10, "Container") if (response == nil) { return } @@ -78,16 +68,11 @@ func query(arguments []string) { } } -func startsByParameter(argument string) bool { - match, _ := regexp.MatchString("[a-zA-Z0-9_]+=", argument) // TODO: Allow dot in parameters - return match -} - -func escapePayload(argument string) string { +func splitArg(argument string) (string, string) { equalsIndex := strings.Index(argument, "=") if equalsIndex < 1 { - utils.Error("A query argument must be on the form parameter=value, but was '" + argument + "'") - return "" + return "yql", argument + } else { + return argument[0:equalsIndex], argument[equalsIndex + 1:len(argument)] } - return argument[0:equalsIndex] + "=" + url.QueryEscape(argument[equalsIndex + 1:len(argument)]) } diff --git a/client/go/src/cmd/query_test.go b/client/go/src/cmd/query_test.go index 3235eae931a..e062e9c718a 100644 --- a/client/go/src/cmd/query_test.go +++ b/client/go/src/cmd/query_test.go @@ -17,7 +17,7 @@ func TestQuery(t *testing.T) { func TestQueryWithMultipleParameters(t *testing.T) { assertQuery(t, - "?yql=select+from+sources+%2A+where+title+contains+%27foo%27&hits=5", + "?hits=5&yql=select+from+sources+%2A+where+title+contains+%27foo%27", "select from sources * where title contains 'foo'", "hits=5") } |