forked from pivotal-cf/om
/
curl.go
110 lines (89 loc) · 3.06 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
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
package commands
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"encoding/json"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/om/api"
)
//go:generate counterfeiter -o ./fakes/request_service.go --fake-name RequestService . requestService
type requestService interface {
Invoke(api.RequestServiceInvokeInput) (api.RequestServiceInvokeOutput, error)
}
type Curl struct {
requestService requestService
stdout logger
stderr logger
Options struct {
Path string `long:"path" short:"p" required:"true" description:"path to api endpoint"`
Method string `long:"request" short:"x" description:"http verb" default:"GET"`
Data string `long:"data" short:"d" description:"api request payload"`
Silent bool `long:"silent" short:"s" description:"only write response headers to stderr if response status is 4XX or 5XX"`
Headers []string `long:"header" short:"H" description:"used to specify custom headers with your command" default:"Content-Type: application/json"`
}
}
func NewCurl(rs requestService, stdout logger, stderr logger) Curl {
return Curl{requestService: rs, stdout: stdout, stderr: stderr}
}
func (c Curl) Execute(args []string) error {
if _, err := jhanda.Parse(&c.Options, args); err != nil {
return fmt.Errorf("could not parse curl flags: %s", err)
}
requestHeaders := make(http.Header)
for _, h := range c.Options.Headers {
split := strings.Split(h, " ")
requestHeaders.Set(strings.TrimSuffix(split[0], ":"), split[1])
}
input := api.RequestServiceInvokeInput{
Path: c.Options.Path,
Method: c.Options.Method,
Data: strings.NewReader(c.Options.Data),
Headers: requestHeaders,
}
output, err := c.requestService.Invoke(input)
if err != nil {
return fmt.Errorf("failed to make api request: %s", err)
}
writeHeadersToStderr := !c.Options.Silent || output.StatusCode >= 400
if writeHeadersToStderr {
c.stderr.Printf("Status: %d %s", output.StatusCode, http.StatusText(output.StatusCode))
}
headers := bytes.NewBuffer([]byte{})
err = output.Headers.Write(headers)
if err != nil {
return fmt.Errorf("failed to write api response headers: %s", err)
}
if writeHeadersToStderr {
c.stderr.Printf(headers.String())
}
body, err := ioutil.ReadAll(output.Body)
if err != nil {
return fmt.Errorf("failed to read api response body: %s", err)
}
for _, contentType := range output.Headers["Content-Type"] {
if strings.HasPrefix(contentType, "application/json") {
var prettyJSON bytes.Buffer
err := json.Indent(&prettyJSON, body, "", " ")
if err != nil {
panic(err)
}
body = prettyJSON.Bytes()
break
}
}
c.stdout.Println(string(body))
if output.StatusCode >= 400 {
return fmt.Errorf("server responded with an error")
}
return nil
}
func (c Curl) Usage() jhanda.Usage {
return jhanda.Usage{
Description: "This command issues an authenticated API request as defined in the arguments",
ShortDescription: "issues an authenticated API request",
Flags: c.Options,
}
}