summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/document.go
blob: ccbd86abfe30b145d6786efc940e80b4904b634d (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
// 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"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"time"

	"github.com/spf13/cobra"
	"github.com/vespa-engine/vespa/util"
	"github.com/vespa-engine/vespa/vespa"
)

func init() {
	rootCmd.AddCommand(documentCmd)
	documentCmd.AddCommand(documentPostCmd)
	documentCmd.AddCommand(documentGetCmd)
}

var documentCmd = &cobra.Command{
	Use:   "document",
	Short: "Issue document operations",
	Example: `$ vespa document src/test/resources/A-Head-Full-of-Dreams.json
# (short-hand for vespa document post)`,
	Args: cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		post("", args[0])
	},
}

var documentPostCmd = &cobra.Command{
	Use:   "post",
	Short: "Posts the document in the given file",
	Args:  cobra.RangeArgs(1, 2),
	Example: `$ vespa document post src/test/resources/A-Head-Full-of-Dreams.json
$ vespa document post id:mynamespace:music::a-head-full-of-dreams src/test/resources/A-Head-Full-of-Dreams.json`,
	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",
	Args:  cobra.ExactArgs(1),
	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 {
		log.Print(color.Red("Error: "), "Could not open file '", color.Cyan(jsonFile), "'")
		log.Print(color.Brown(fileError))
		return
	}

	documentData := util.ReaderToBytes(fileReader)

	if documentId == "" {
		var doc map[string]interface{}
		json.Unmarshal(documentData, &doc)
		if doc["put"] != nil {
			documentId = doc["put"].(string) // document feeder format
		} else {
			log.Print(color.Red("Error: "), "No document id given neither as argument or as a 'put' key in the json file")
			return
		}
	}

	documentPath, documentPathError := vespa.IdToURLPath(documentId)
	if documentPathError != nil {
		log.Print(color.Red("Error: "), "Invalid document id '", color.Red(documentId), "': ", documentPathError)
		return
	}

	url, urlParseError := url.Parse(documentTarget() + "/document/v1/" + documentPath)
	if urlParseError != nil {
		log.Print(color.Red("Error: "), "Invalid request path: '", color.Red(documentTarget()+"/document/v1/"+documentPath), "': ", urlParseError)
		return
	}

	request := &http.Request{
		URL:    url,
		Method: "POST",
		Header: header,
		Body:   ioutil.NopCloser(bytes.NewReader(documentData)),
	}
	serviceDescription := "Container (document API)"
	response, err := util.HttpDo(request, time.Second*60, serviceDescription)
	if response == nil {
		log.Print(color.Red("Error: "), "Request failed:", err)
		return
	}

	defer response.Body.Close()
	if response.StatusCode == 200 {
		log.Print(color.Green(documentId))
	} else if response.StatusCode/100 == 4 {
		log.Print(color.Red("Error: "), "Invalid document: ", response.Status, "\n\n")
		log.Print(util.ReaderToJSON(response.Body))
	} else {
		log.Print(color.Red("Error: "), serviceDescription, " at ", color.Cyan(request.URL.Host), ": ", response.Status, "\n\n")
		log.Print(util.ReaderToJSON(response.Body))
	}
}