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
|
// 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 (
"github.com/spf13/cobra"
"github.com/vespa-engine/vespa/utils"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
)
func init() {
rootCmd.AddCommand(documentCmd)
statusCmd.AddCommand(documentPutCmd)
statusCmd.AddCommand(documentGetCmd)
}
var documentCmd = &cobra.Command{
Use: "document",
Short: "Issue document operations (put by default)",
Long: `TODO: Example mynamespace/mydocumenttype/myid document.json`,
// TODO: Check args
Run: func(cmd *cobra.Command, args []string) {
put(args[0], args[1])
},
}
var documentPutCmd = &cobra.Command{
Use: "put mynamespace/mydocumenttype/myid mydocument.json",
Short: "Puts 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])
},
}
var documentGetCmd = &cobra.Command{
Use: "get documentId",
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 put(documentId string, jsonFile string) {
url, _ := url.Parse(getTarget(documentContext).document + "/document/v1/" + documentId)
header := http.Header{}
header.Add("Content-Type", "application/json")
fileReader, fileError := os.Open(jsonFile)
if fileError != nil {
utils.Error("Could not open file at " + jsonFile)
utils.Detail(fileError.Error())
return
}
request := &http.Request{
URL: url,
Method: "POST",
Header: header,
Body: ioutil.NopCloser(fileReader),
}
serviceDescription := "Container (document/v1 API)"
response := utils.HttpDo(request, time.Second * 60, serviceDescription)
defer response.Body.Close()
if (response == nil) {
return
} else if response.StatusCode == 200 {
utils.Success("Success") // TODO: Change to something including document id
} else if response.StatusCode % 100 == 4 {
utils.Error("Invalid document")
utils.Detail(response.Status)
// TODO: Output error in body
} else {
utils.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host)
utils.Detail("Response status:", response.Status)
}
}
|