aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/query.go
blob: bf2272ca98106426c6c179163f44e8080868464f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa query command
// author: bratseth

package cmd

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"strings"
	"time"

	"github.com/fatih/color"
	"github.com/spf13/cobra"
	"github.com/vespa-engine/vespa/client/go/internal/curl"
	"github.com/vespa-engine/vespa/client/go/internal/util"
	"github.com/vespa-engine/vespa/client/go/internal/vespa"
)

func newQueryCmd(cli *CLI) *cobra.Command {
	var (
		printCurl        bool
		queryTimeoutSecs int
		waitSecs         int
	)
	cmd := &cobra.Command{
		Use:     "query query-parameters",
		Short:   "Issue a query to Vespa",
		Example: `$ vespa query "yql=select * from music where album contains 'head'" hits=5`,
		Long: `Issue a query to Vespa.

Any parameter from https://docs.vespa.ai/en/reference/query-api-reference.html
can be set by the syntax [parameter-name]=[value].`,
		// TODO: Support referencing a query json file
		DisableAutoGenTag: true,
		SilenceUsage:      true,
		Args:              cobra.MinimumNArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return query(cli, args, queryTimeoutSecs, waitSecs, printCurl)
		},
	}
	cmd.PersistentFlags().BoolVarP(&printCurl, "verbose", "v", false, "Print the equivalent curl command for the query")
	cmd.Flags().IntVarP(&queryTimeoutSecs, "timeout", "T", 10, "Timeout for the query in seconds")
	cli.bindWaitFlag(cmd, 0, &waitSecs)
	return cmd
}

func printCurl(stderr io.Writer, url string, service *vespa.Service) error {
	cmd, err := curl.RawArgs(url)
	if err != nil {
		return err
	}
	cmd.Certificate = service.TLSOptions.CertificateFile
	cmd.PrivateKey = service.TLSOptions.PrivateKeyFile
	_, err = io.WriteString(stderr, cmd.String()+"\n")
	return err
}

func query(cli *CLI, arguments []string, timeoutSecs, waitSecs int, curl bool) error {
	target, err := cli.target(targetOptions{})
	if err != nil {
		return err
	}
	waiter := cli.waiter(time.Duration(waitSecs) * time.Second)
	service, err := waiter.Service(target, cli.config.cluster())
	if err != nil {
		return err
	}
	url, _ := url.Parse(service.BaseURL + "/search/")
	urlQuery := url.Query()
	for i := 0; i < len(arguments); i++ {
		key, value := splitArg(arguments[i])
		urlQuery.Set(key, value)
	}
	queryTimeout := urlQuery.Get("timeout")
	if queryTimeout == "" {
		// No timeout set by user, use the timeout option
		queryTimeout = fmt.Sprintf("%ds", timeoutSecs)
		urlQuery.Set("timeout", queryTimeout)
	}
	url.RawQuery = urlQuery.Encode()
	deadline, err := time.ParseDuration(queryTimeout)
	if err != nil {
		return fmt.Errorf("invalid query timeout: %w", err)
	}
	if curl {
		if err := printCurl(cli.Stderr, url.String(), service); err != nil {
			return err
		}
	}
	response, err := service.Do(&http.Request{URL: url}, deadline+time.Second) // Slightly longer than query timeout
	if err != nil {
		return fmt.Errorf("request failed: %w", err)
	}
	defer response.Body.Close()

	if response.StatusCode == 200 {
		log.Print(util.ReaderToJSON(response.Body))
	} else if response.StatusCode/100 == 4 {
		return fmt.Errorf("invalid query: %s\n%s", response.Status, util.ReaderToJSON(response.Body))
	} else {
		return fmt.Errorf("%s from container at %s\n%s", response.Status, color.CyanString(url.Host), util.ReaderToJSON(response.Body))
	}
	return nil
}

func splitArg(argument string) (string, string) {
	parts := strings.SplitN(argument, "=", 2)
	if len(parts) < 2 {
		return "yql", parts[0]
	}
	if strings.HasPrefix(strings.ToLower(parts[0]), "select ") {
		// A query containing '='
		return "yql", argument
	}
	return parts[0], parts[1]
}