-
Notifications
You must be signed in to change notification settings - Fork 929
/
curl.go
121 lines (103 loc) · 3.28 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
111
112
113
114
115
116
117
118
119
120
121
package commands
import (
"bytes"
"encoding/json"
"errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/flag_helpers"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
"github.com/codegangsta/cli"
)
type Curl struct {
ui terminal.UI
config core_config.Reader
curlRepo api.CurlRepository
}
func NewCurl(ui terminal.UI, config core_config.Reader, curlRepo api.CurlRepository) (cmd *Curl) {
cmd = new(Curl)
cmd.ui = ui
cmd.config = config
cmd.curlRepo = curlRepo
return
}
func (cmd *Curl) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "curl",
Description: T("Executes a raw request, content-type set to application/json by default"),
Usage: T("CF_NAME curl PATH [-iv] [-X METHOD] [-H HEADER] [-d DATA] [--output FILE]"),
Flags: []cli.Flag{
cli.BoolFlag{Name: "i", Usage: T("Include response headers in the output")},
cli.BoolFlag{Name: "v", Usage: T("Enable CF_TRACE output for all requests and responses")},
cli.StringFlag{Name: "X", Value: "GET", Usage: T("HTTP method (GET,POST,PUT,DELETE,etc)")},
flag_helpers.NewStringSliceFlag("H", T("Custom headers to include in the request, flag can be specified multiple times")),
flag_helpers.NewStringFlag("d", T("HTTP data to include in the request body")),
flag_helpers.NewStringFlag("output", T("Write curl body to FILE instead of stdout")),
},
}
}
func (cmd *Curl) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 1 {
err = errors.New(T("Incorrect number of arguments"))
cmd.ui.FailWithUsage(c)
return
}
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return
}
func (cmd *Curl) Run(c *cli.Context) {
path := c.Args()[0]
method := c.String("X")
headers := c.StringSlice("H")
body := c.String("d")
verbose := c.Bool("v")
reqHeader := strings.Join(headers, "\n")
if verbose {
trace.EnableTrace()
}
responseHeader, responseBody, apiErr := cmd.curlRepo.Request(method, path, reqHeader, body)
if apiErr != nil {
cmd.ui.Failed(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()}))
}
if verbose {
return
}
if c.Bool("i") {
cmd.ui.Say(responseHeader)
}
if c.String("output") != "" {
err := cmd.writeToFile(responseBody, c.String("output"))
if err != nil {
cmd.ui.Failed(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": err}))
}
} else {
if strings.Contains(responseHeader, "application/json") {
buffer := bytes.Buffer{}
err := json.Indent(&buffer, []byte(responseBody), "", " ")
if err == nil {
responseBody = buffer.String()
}
}
cmd.ui.Say(responseBody)
}
return
}
func (cmd Curl) writeToFile(responseBody, filePath string) (err error) {
if _, err = os.Stat(filePath); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(filePath), 0755)
}
if err != nil {
return
}
return ioutil.WriteFile(filePath, []byte(responseBody), 0644)
}