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
|
// 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 (
"log"
"strings"
"github.com/spf13/cobra"
"github.com/vespa-engine/vespa/util"
"github.com/vespa-engine/vespa/vespa"
)
func init() {
rootCmd.AddCommand(documentCmd)
documentCmd.AddCommand(documentPutCmd)
documentCmd.AddCommand(documentUpdateCmd)
documentCmd.AddCommand(documentRemoveCmd)
documentCmd.AddCommand(documentGetCmd)
}
var documentCmd = &cobra.Command{
Use: "document",
Short: "Issues the document operation in the given file to Vespa",
Example: `$ vespa document src/test/resources/A-Head-Full-of-Dreams.json`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
printResult(vespa.Send(args[0], documentTarget()), false)
},
}
var documentPutCmd = &cobra.Command{
Use: "put",
Short: "Writes the document in the given file to Vespa",
Args: cobra.RangeArgs(1, 2),
Example: `$ vespa document put src/test/resources/A-Head-Full-of-Dreams.json
$ vespa document put 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 {
printResult(vespa.Put("", args[0], documentTarget()), false)
} else {
printResult(vespa.Put(args[0], args[1], documentTarget()), false)
}
},
}
var documentUpdateCmd = &cobra.Command{
Use: "update",
Short: "Modifies some fields of an existing document as specified in the given file",
Args: cobra.RangeArgs(1, 2),
Example: `$ vespa document update src/test/resources/A-Head-Full-of-Dreams-Update.json
$ vespa document update 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 {
printResult(vespa.Update("", args[0], documentTarget()), false)
} else {
printResult(vespa.Update(args[0], args[1], documentTarget()), false)
}
},
}
var documentRemoveCmd = &cobra.Command{
Use: "remove",
Short: "Removes a document from Vespa",
Args: cobra.ExactArgs(1),
Example: `$ vespa document remove src/test/resources/A-Head-Full-of-Dreams-Remove.json
$ vespa document remove id:mynamespace:music::a-head-full-of-dreams`,
Run: func(cmd *cobra.Command, args []string) {
if strings.HasPrefix(args[0], "id:") {
printResult(vespa.RemoveId(args[0], documentTarget()), false)
} else {
printResult(vespa.RemoveOperation(args[0], documentTarget()), false)
}
},
}
var documentGetCmd = &cobra.Command{
Use: "get",
Short: "Gets a document",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
printResult(vespa.Get(args[0], documentTarget()), true)
},
}
func printResult(result util.OperationResult, payloadOnlyOnSuccess bool) {
if !result.Success {
log.Print(color.Red("Error: "), result.Message)
} else if !(payloadOnlyOnSuccess && result.Payload != "") {
log.Print(color.Green("Success: "), result.Message)
}
if result.Detail != "" {
log.Print(color.Brown(result.Detail))
}
if result.Payload != "" {
if !payloadOnlyOnSuccess {
log.Println("")
}
log.Print(result.Payload)
}
}
|