summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/curl.go
blob: 4d949b51e8f9dd87562fb66d928ad1cf24b5035b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"

	"github.com/kballard/go-shellquote"
	"github.com/spf13/cobra"
	"github.com/vespa-engine/vespa/client/go/vespa"
)

var curlDryRun bool
var curlPath string

func init() {
	rootCmd.AddCommand(curlCmd)
	curlCmd.Flags().StringVarP(&curlPath, "path", "p", "", "The path to curl. If this is unset, curl from PATH is used")
	curlCmd.Flags().BoolVarP(&curlDryRun, "dry-run", "n", false, "Print the curl command that would be executed")
}

var curlCmd = &cobra.Command{
	Use:   "curl [curl-options] path",
	Short: "Query Vespa using curl",
	Long: `Query Vespa using curl.

Execute curl with the appropriate URL, certificate and private key for your application.`,
	Example: `$ vespa curl /search/?yql=query
$ vespa curl -- -v --data-urlencode "yql=select * from sources * where title contains 'foo';" /search/
$ vespa curl -t local -- -v /search/?yql=query
`,
	DisableAutoGenTag: true,
	Args:              cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		cfg, err := LoadConfig()
		if err != nil {
			fatalErr(err, "Could not load config")
			return
		}
		app, err := vespa.ApplicationFromString(getApplication())
		if err != nil {
			fatalErr(err)
			return
		}
		privateKeyFile, err := cfg.PrivateKeyPath(app)
		if err != nil {
			fatalErr(err)
			return
		}
		certificateFile, err := cfg.CertificatePath(app)
		if err != nil {
			fatalErr(err)
			return
		}
		service := getService("query", 0)
		c := &curl{privateKeyPath: privateKeyFile, certificatePath: certificateFile}
		if curlDryRun {
			cmd, err := c.command(service.BaseURL, args...)
			if err != nil {
				fatalErr(err, "Failed to create curl command")
				return
			}
			log.Print(shellquote.Join(cmd.Args...))
		} else {
			if err := c.run(service.BaseURL, args...); err != nil {
				fatalErr(err, "Failed to run curl")
				return
			}
		}
	},
}

type curl struct {
	path            string
	certificatePath string
	privateKeyPath  string
}

func (c *curl) run(baseURL string, args ...string) error {
	cmd, err := c.command(baseURL, args...)
	if err != nil {
		return err
	}
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Start(); err != nil {
		return err
	}
	return cmd.Wait()
}

func (c *curl) command(baseURL string, args ...string) (*exec.Cmd, error) {
	if len(args) == 0 {
		return nil, fmt.Errorf("need at least one argument")
	}

	if c.path == "" {
		resolvedPath, err := resolveCurlPath()
		if err != nil {
			return nil, err
		}
		c.path = resolvedPath
	}

	path := args[len(args)-1]
	args = args[:len(args)-1]
	if !hasOption("--key", args) && c.privateKeyPath != "" {
		args = append(args, "--key", c.privateKeyPath)
	}
	if !hasOption("--cert", args) && c.certificatePath != "" {
		args = append(args, "--cert", c.certificatePath)
	}

	baseURL = strings.TrimSuffix(baseURL, "/")
	path = strings.TrimPrefix(path, "/")
	args = append(args, baseURL+"/"+path)

	return exec.Command(c.path, args...), nil
}

func hasOption(option string, args []string) bool {
	for _, arg := range args {
		if arg == option {
			return true
		}
	}
	return false
}

func resolveCurlPath() (string, error) {
	var curlPath string
	var err error
	curlPath, err = exec.LookPath("curl")
	if err != nil {
		curlPath, err = exec.LookPath("curl.exe")
		if err != nil {
			return "", err
		}
	}
	return curlPath, nil
}