diff options
Diffstat (limited to 'client/go/cmd/curl.go')
-rw-r--r-- | client/go/cmd/curl.go | 110 |
1 files changed, 53 insertions, 57 deletions
diff --git a/client/go/cmd/curl.go b/client/go/cmd/curl.go index 289a65465bd..e06942196a6 100644 --- a/client/go/cmd/curl.go +++ b/client/go/cmd/curl.go @@ -8,81 +8,77 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/client/go/auth0" + "github.com/vespa-engine/vespa/client/go/auth/auth0" "github.com/vespa-engine/vespa/client/go/curl" "github.com/vespa-engine/vespa/client/go/vespa" ) -var curlDryRun bool -var curlService string - -func init() { - rootCmd.AddCommand(curlCmd) - curlCmd.Flags().BoolVarP(&curlDryRun, "dry-run", "n", false, "Print the curl command that would be executed") - curlCmd.Flags().StringVarP(&curlService, "service", "s", "query", "Which service to query. Must be \"deploy\", \"document\" or \"query\"") -} - -var curlCmd = &cobra.Command{ - Use: "curl [curl-options] path", - Short: "Access Vespa directly using curl", - Long: `Access Vespa directly using curl. +func newCurlCmd(cli *CLI) *cobra.Command { + var ( + dryRun bool + curlService string + ) + cmd := &cobra.Command{ + Use: "curl [curl-options] path", + Short: "Access Vespa directly using curl", + Long: `Access Vespa directly using curl. Execute curl with the appropriate URL, certificate and private key for your application. For a more high-level interface to query and feeding, see the 'query' and 'document' commands. `, - Example: `$ vespa curl /ApplicationStatus + Example: `$ vespa curl /ApplicationStatus $ vespa curl -- -X POST -H "Content-Type:application/json" --data-binary @src/test/resources/A-Head-Full-of-Dreams.json /document/v1/namespace/music/docid/1 $ vespa curl -- -v --data-urlencode "yql=select * from music where album contains 'head';" /search/\?hits=5`, - DisableAutoGenTag: true, - SilenceUsage: true, - Args: cobra.MinimumNArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := LoadConfig() - if err != nil { - return err - } - target, err := getTarget() - if err != nil { - return err - } - service, err := target.Service(curlService, 0, 0, "") - if err != nil { - return err - } - url := joinURL(service.BaseURL, args[len(args)-1]) - rawArgs := args[:len(args)-1] - c, err := curl.RawArgs(url, rawArgs...) - if err != nil { - return err - } - switch curlService { - case vespa.DeployService: - if target.Type() == vespa.TargetCloud { - if err := addCloudAuth0Authentication(target.Deployment().System, cfg, c); err != nil { - return err + DisableAutoGenTag: true, + SilenceUsage: true, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + target, err := cli.target(targetOptions{}) + if err != nil { + return err + } + service, err := target.Service(curlService, 0, 0, "") + if err != nil { + return err + } + url := joinURL(service.BaseURL, args[len(args)-1]) + rawArgs := args[:len(args)-1] + c, err := curl.RawArgs(url, rawArgs...) + if err != nil { + return err + } + switch curlService { + case vespa.DeployService: + if target.Type() == vespa.TargetCloud { + if err := addCloudAuth0Authentication(target.Deployment().System, cli.config, c); err != nil { + return err + } } + case vespa.DocumentService, vespa.QueryService: + c.PrivateKey = service.TLSOptions.PrivateKeyFile + c.Certificate = service.TLSOptions.CertificateFile + default: + return fmt.Errorf("service not found: %s", curlService) } - case vespa.DocumentService, vespa.QueryService: - c.PrivateKey = service.TLSOptions.PrivateKeyFile - c.Certificate = service.TLSOptions.CertificateFile - default: - return fmt.Errorf("service not found: %s", curlService) - } - if curlDryRun { - log.Print(c.String()) - } else { - if err := c.Run(os.Stdout, os.Stderr); err != nil { - return fmt.Errorf("failed to execute curl: %w", err) + if dryRun { + log.Print(c.String()) + } else { + if err := c.Run(os.Stdout, os.Stderr); err != nil { + return fmt.Errorf("failed to execute curl: %w", err) + } } - } - return nil - }, + return nil + }, + } + cmd.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "Print the curl command that would be executed") + cmd.Flags().StringVarP(&curlService, "service", "s", "query", "Which service to query. Must be \"deploy\", \"document\" or \"query\"") + return cmd } func addCloudAuth0Authentication(system vespa.System, cfg *Config, c *curl.Command) error { - a, err := auth0.GetAuth0(cfg.AuthConfigPath(), system.Name, system.URL) + a, err := auth0.GetAuth0(cfg.authConfigPath(), system.Name, system.URL) if err != nil { return err } |