-
Notifications
You must be signed in to change notification settings - Fork 928
/
curl.go
156 lines (129 loc) · 4.61 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
145
146
147
148
149
150
151
152
153
154
155
156
package commands
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"code.cloudfoundry.org/cli/cf/flagcontext"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
cfErrors "code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/trace"
)
type Curl struct {
ui terminal.UI
config coreconfig.Reader
curlRepo api.CurlRepository
pluginCall bool
}
func init() {
commandregistry.Register(&Curl{})
}
func (cmd *Curl) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["i"] = &flags.BoolFlag{ShortName: "i", Usage: T("Include response headers in the output")}
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")}
fs["fail"] = &flags.BoolFlag{ShortName: "f", Name: "fail", Usage: "Server errors return exit code 22"}
return commandregistry.CommandMetadata{
Name: "curl",
Description: T("Executes a request to the targeted API endpoint"),
Usage: []string{
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: []string{
`CF_NAME curl "/v2/apps" -X GET -H "Content-Type: application/x-www-form-urlencoded" -d 'q=name:myapp'`,
`CF_NAME curl "/v2/apps" -d @/path/to/file`,
},
Flags: fs,
}
}
func (cmd *Curl) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. An argument is missing or not correctly enclosed.\n\n") + commandregistry.Commands.CommandUsage("curl"))
return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
}
reqs := []requirements.Requirement{
requirementsFactory.NewAPIEndpointRequirement(),
}
return reqs, nil
}
func (cmd *Curl) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.curlRepo = deps.RepoLocator.GetCurlRepository()
cmd.pluginCall = pluginCall
return cmd
}
func (cmd *Curl) Execute(c flags.FlagContext) error {
path := c.Args()[0]
headers := c.StringSlice("H")
var method string
var body string
if c.IsSet("d") {
method = "POST"
jsonBytes, err := flagcontext.GetContentsFromOptionalFlagValue(c.String("d"))
if err != nil {
return err
}
body = string(jsonBytes)
}
if c.IsSet("X") {
method = c.String("X")
}
reqHeader := strings.Join(headers, "\n")
responseHeader, responseBody, err := cmd.curlRepo.Request(method, path, reqHeader, body, c.Bool("fail"))
if err != nil {
if httpErr, ok := err.(cfErrors.HTTPError); ok && c.Bool("fail") {
return cfErrors.NewCurlHTTPError(httpErr.StatusCode())
}
return errors.New(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": err.Error()}))
}
if trace.LoggingToStdout && !cmd.pluginCall {
return nil
}
if c.Bool("i") {
cmd.ui.Say(responseHeader)
}
if c.String("output") != "" {
err := cmd.writeToFile(responseBody, c.String("output"))
if err != nil {
return errors.New(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 nil
}
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)
}