/
curl.go
69 lines (53 loc) · 1.59 KB
/
curl.go
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
package cmd
import (
"net/http"
"net/http/httputil"
. "github.com/cloudfoundry/bosh-cli/v7/cmd/opts"
boshdir "github.com/cloudfoundry/bosh-cli/v7/director"
boshui "github.com/cloudfoundry/bosh-cli/v7/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type CurlCmd struct {
ui boshui.UI
clientRequest boshdir.ClientRequest
}
func NewCurlCmd(ui boshui.UI, clientRequest boshdir.ClientRequest) CurlCmd {
return CurlCmd{ui: ui, clientRequest: clientRequest}
}
func (c CurlCmd) Run(opts CurlOpts) error {
updateReq := func(req *http.Request) {
for _, h := range opts.Headers {
req.Header.Add(h.Name, h.Value)
}
}
var bodyBs []byte
var resp *http.Response
var respErr error
switch opts.Method {
case "GET":
bodyBs, resp, respErr = c.clientRequest.RawGet(opts.Args.Path, nil, updateReq)
case "POST":
bodyBs, resp, respErr = c.clientRequest.RawPost(opts.Args.Path, opts.Body.Bytes, updateReq)
case "PUT":
bodyBs, resp, respErr = c.clientRequest.RawPut(opts.Args.Path, opts.Body.Bytes, updateReq)
case "DELETE":
if len(opts.Headers) > 0 {
return bosherr.Errorf("Expected no headers")
}
bodyBs, resp, respErr = c.clientRequest.RawDelete(opts.Args.Path)
default:
return bosherr.Errorf("Unknown method '%s'", opts.Method)
}
if opts.ShowHeaders {
restBs, serializeErr := httputil.DumpResponse(resp, false)
if serializeErr != nil {
return bosherr.WrapErrorf(serializeErr, "Dumping HTTP response")
}
c.ui.PrintBlock(restBs)
}
c.ui.PrintBlock(bodyBs)
if respErr != nil {
return bosherr.WrapErrorf(respErr, "Executing HTTP request")
}
return nil
}