1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa document command
// author: bratseth
package cmd
import (
"bytes"
"encoding/json"
"github.com/spf13/cobra"
"github.com/vespa-engine/vespa/util"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
)
func init() {
rootCmd.AddCommand(documentCmd)
documentCmd.AddCommand(documentPostCmd)
documentCmd.AddCommand(documentGetCmd)
}
var documentCmd = &cobra.Command{
Use: "document",
Short: "Issue document operations",
Long: `TODO: Example vespa document mynamespace/mydocumenttype/myid document.json`,
// TODO: Check args
Run: func(cmd *cobra.Command, args []string) {
post("", args[0])
},
}
var documentPostCmd = &cobra.Command{
Use: "post",
Short: "Posts the document in the given file",
Long: `TODO`,
// TODO: Check args
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
post("", args[0])
} else {
post(args[0], args[1])
}
},
}
var documentGetCmd = &cobra.Command{
Use: "get",
Short: "Gets a document",
Long: `TODO`,
// TODO: Check args
Run: func(cmd *cobra.Command, args []string) {
get(args[0])
},
}
func get(documentId string) {
// TODO
}
func post(documentId string, jsonFile string) {
header := http.Header{}
header.Add("Content-Type", "application/json")
fileReader, fileError := os.Open(jsonFile)
if fileError != nil {
util.Error("Could not open file at " + jsonFile)
util.Detail(fileError.Error())
return
}
documentData := util.ReaderToBytes(fileReader)
if documentId == "" {
var doc map[string]interface{}
json.Unmarshal(documentData, &doc)
if doc["id"] != nil {
documentId = doc["id"].(string)
} else if doc["put"] != nil {
documentId = doc["put"].(string) // document feeder format
} else {
util.Error("No document id given neither as argument or an 'id' key in the json file")
return
}
}
url, _ := url.Parse(documentTarget() + "/document/v1/" + documentId)
request := &http.Request{
URL: url,
Method: "POST",
Header: header,
Body: ioutil.NopCloser(bytes.NewReader(documentData)),
}
serviceDescription := "Container (document API)"
response := util.HttpDo(request, time.Second*60, serviceDescription)
if response == nil {
return
}
defer response.Body.Close()
if response.StatusCode == 200 {
util.Success("Success") // TODO: Change to something including document id
} else if response.StatusCode/100 == 4 {
util.Error("Invalid document (" + response.Status + "):")
util.PrintReader(response.Body)
} else {
util.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host, "("+response.Status+"):")
util.PrintReader(response.Body)
}
}
|