From 4478aeeb245a208c8e4a4c4c9d217136de03f609 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 24 Aug 2021 09:50:31 +0200 Subject: Move modules to root --- client/go/build.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'client/go/build.sh') diff --git a/client/go/build.sh b/client/go/build.sh index 9edc5df4002..08d8f1d8983 100755 --- a/client/go/build.sh +++ b/client/go/build.sh @@ -1,7 +1,14 @@ # Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. # Execute from this directory to build the command-line client to bin/vespa -export GOPATH=`pwd` -cd "$GOPATH/src/" -go test ./... +cd util +go test +cd .. + +cd cmd +go test +cd .. + +export GOBIN=`pwd`/bin go install + -- cgit v1.2.3 From 582b666d2aaa51749555193637be7fa0a7a1dc24 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 24 Aug 2021 13:45:32 +0200 Subject: document get -> post and allow id in document --- client/go/build.sh | 9 +---- client/go/cmd/document.go | 47 +++++++++++++++------- client/go/cmd/document_test.go | 20 +++++---- .../testdata/A-Head-Full-of-Dreams-With-Id.json | 15 +++++++ 4 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 client/go/cmd/testdata/A-Head-Full-of-Dreams-With-Id.json (limited to 'client/go/build.sh') diff --git a/client/go/build.sh b/client/go/build.sh index 08d8f1d8983..6d9416e8950 100755 --- a/client/go/build.sh +++ b/client/go/build.sh @@ -1,14 +1,7 @@ # Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. # Execute from this directory to build the command-line client to bin/vespa -cd util -go test -cd .. - -cd cmd -go test -cd .. - export GOBIN=`pwd`/bin +go test ./... go install diff --git a/client/go/cmd/document.go b/client/go/cmd/document.go index f48a3965d01..9f9d6a9752c 100644 --- a/client/go/cmd/document.go +++ b/client/go/cmd/document.go @@ -5,6 +5,8 @@ package cmd import ( + "bytes" + "encoding/json" "github.com/spf13/cobra" "github.com/vespa-engine/vespa/util" "io/ioutil" @@ -17,34 +19,37 @@ import ( func init() { rootCmd.AddCommand(documentCmd) - statusCmd.AddCommand(documentPutCmd) - statusCmd.AddCommand(documentGetCmd) + documentCmd.AddCommand(documentPostCmd) + documentCmd.AddCommand(documentGetCmd) } var documentCmd = &cobra.Command{ Use: "document", - Short: "Issue document operations (put by default)", + Short: "Issue document operations", Long: `TODO: Example mynamespace/mydocumenttype/myid document.json`, // TODO: Check args Run: func(cmd *cobra.Command, args []string) { - put(args[0], args[1]) + util.Error("Use either document post or document get") }, } -var documentPutCmd = &cobra.Command{ - Use: "put mynamespace/mydocumenttype/myid mydocument.json", - Short: "Puts the document in the given file", +var documentPostCmd = &cobra.Command{ + Use: "post", + Short: "Posts the document in the given file", Long: `TODO`, - // TODO: This crashes with the above // TODO: Extract document id from the content // TODO: Check args Run: func(cmd *cobra.Command, args []string) { - put(args[0], args[1]) + if len(args) == 10 { + post("", args[0]) + } else { + post(args[0], args[1]) + } }, } var documentGetCmd = &cobra.Command{ - Use: "get documentId", + Use: "get", Short: "Gets a document", Long: `TODO`, // TODO: Check args @@ -57,8 +62,7 @@ func get(documentId string) { // TODO } -func put(documentId string, jsonFile string) { - // TODO: Support document id in JSON, see https://docs.vespa.ai/en/reference/document-json-format.html +func post(documentId string, jsonFile string) { url, _ := url.Parse(getTarget(documentContext).document + "/document/v1/" + documentId) header := http.Header{} @@ -71,11 +75,26 @@ func put(documentId string, jsonFile string) { return } + documentData := util.ReaderToBytes(fileReader) + + if documentId == "" { + var doc map[string]interface{} + json.Unmarshal(documentData, &doc) + documentId = doc["id"].(string) + if documentId == "" { + documentId = doc["put"].(string) // document feeder format + } + if documentId == "" { + util.Error("No document id given neither as argument or an 'id' key in the json file") + return + } + } + request := &http.Request{ URL: url, Method: "POST", Header: header, - Body: ioutil.NopCloser(fileReader), + Body: ioutil.NopCloser(bytes.NewReader(documentData)), } serviceDescription := "Container (document API)" response := util.HttpDo(request, time.Second * 60, serviceDescription) @@ -93,4 +112,4 @@ func put(documentId string, jsonFile string) { util.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host, "(" + response.Status + "):") util.PrintReader(response.Body) } -} \ No newline at end of file +} diff --git a/client/go/cmd/document_test.go b/client/go/cmd/document_test.go index 9d0ae6e505e..410cfbb9bfd 100644 --- a/client/go/cmd/document_test.go +++ b/client/go/cmd/document_test.go @@ -12,23 +12,27 @@ import ( "testing" ) -func TestDocumentPut(t *testing.T) { - assertDocumentPut("mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json", t) +func TestDocumentPostWithIdArg(t *testing.T) { + assertDocumentPost("mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json", t) } -func TestDocumentPutDocumentError(t *testing.T) { +func TestDocumentPostWithIdInDocument(t *testing.T) { + assertDocumentPost("", "testdata/A-Head-Full-of-Dreams-With-Id.json", t) +} + +func TestDocumentPostDocumentError(t *testing.T) { assertDocumentError(t, 401, "Document error") } -func TestDocumentPutServerError(t *testing.T) { +func TestDocumentPostServerError(t *testing.T) { assertDocumentServerError(t, 501, "Server error") } -func assertDocumentPut(documentId string, jsonFile string, t *testing.T) { +func assertDocumentPost(documentId string, jsonFile string, t *testing.T) { client := &mockHttpClient{} assert.Equal(t, "\x1b[32mSuccess\n", - executeCommand(t, client, []string{"document", documentId, jsonFile}, []string{})) + executeCommand(t, client, []string{"document", "post", documentId, jsonFile}, []string{})) target := getTarget(documentContext).document assert.Equal(t, target + "/document/v1/" + documentId, client.lastRequest.URL.String()) assert.Equal(t, "application/json", client.lastRequest.Header.Get("Content-Type")) @@ -42,7 +46,7 @@ func assertDocumentError(t *testing.T, status int, errorMessage string) { client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } assert.Equal(t, "\x1b[31mInvalid document (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"document", + executeCommand(t, client, []string{"document", "post", "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json"}, []string{})) } @@ -51,7 +55,7 @@ func assertDocumentServerError(t *testing.T, status int, errorMessage string) { client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } assert.Equal(t, "\x1b[31mError from container (document api) at 127.0.0.1:8080 (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"document", + executeCommand(t, client, []string{"document", "post", "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json"}, []string{})) } \ No newline at end of file diff --git a/client/go/cmd/testdata/A-Head-Full-of-Dreams-With-Id.json b/client/go/cmd/testdata/A-Head-Full-of-Dreams-With-Id.json new file mode 100644 index 00000000000..fddbbf94916 --- /dev/null +++ b/client/go/cmd/testdata/A-Head-Full-of-Dreams-With-Id.json @@ -0,0 +1,15 @@ +{ + "id": "mynamespace/music/docid/1", + "fields": { + "album": "A Head Full of Dreams", + "artist": "Coldplay", + "year": 2015, + "category_scores": { + "cells": [ + { "address" : { "cat" : "pop" }, "value": 1 }, + { "address" : { "cat" : "rock" }, "value": 0.2 }, + { "address" : { "cat" : "jazz" }, "value": 0 } + ] + } + } +} -- cgit v1.2.3