forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl.go
144 lines (119 loc) · 4.22 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package commands
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/util"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
)
type Curl struct {
ui terminal.UI
config core_config.Reader
curlRepo api.CurlRepository
}
func init() {
command_registry.Register(&Curl{})
}
func (cmd *Curl) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["i"] = &flags.BoolFlag{ShortName: "i", Usage: T("Include response headers in the output")}
fs["v"] = &flags.BoolFlag{ShortName: "v", Usage: T("Enable CF_TRACE output for all requests and responses")}
fs["X"] = &flags.StringFlag{ShortName: "X", Usage: T("HTTP method (GET,POST,PUT,DELETE,etc)")}
fs["H"] = &flags.StringSliceFlag{ShortName: "H", Usage: T("Custom headers to include in the request, flag can be specified multiple times")}
fs["d"] = &flags.StringFlag{ShortName: "d", Usage: T("HTTP data to include in the request body, or '@' followed by a file name to read the data from")}
fs["output"] = &flags.StringFlag{Name: "output", Usage: T("Write curl body to FILE instead of stdout")}
return command_registry.CommandMetadata{
Name: "curl",
Description: T("Executes a request to the targeted API endpoint"),
Usage: T(`CF_NAME curl PATH [-iv] [-X METHOD] [-H HEADER] [-d DATA] [--output FILE]
By default 'CF_NAME curl' will perform a GET to the specified PATH. If data
is provided via -d, a POST will be performed instead, and the Content-Type
will be set to application/json. You may override headers with -H and the
request method with -X.
For API documentation, please visit http://apidocs.cloudfoundry.org.
EXAMPLES:
cf curl "/v2/apps" -X GET \
-H "Content-Type: application/x-www-form-urlencoded" \
-d 'q=name:myapp'
cf curl "/v2/apps" -d @/path/to/file`),
Flags: fs,
}
}
func (cmd *Curl) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. An argument is missing or not correctly enclosed.\n\n") + command_registry.Commands.CommandUsage("curl"))
}
reqs = []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
}
return
}
func (cmd *Curl) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.curlRepo = deps.RepoLocator.GetCurlRepository()
return cmd
}
func (cmd *Curl) Execute(c flags.FlagContext) {
path := c.Args()[0]
method := c.String("X")
headers := c.StringSlice("H")
var body string
if c.IsSet("d") {
jsonBytes, err := util.GetContentsFromFlagValue(c.String("d"))
if err != nil {
cmd.ui.Failed(err.Error())
}
body = string(jsonBytes)
}
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)
}