forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl.go
144 lines (118 loc) · 4.09 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"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cloudfoundry/cli/cf/flags"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/util"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"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 coreconfig.Reader
curlRepo api.CurlRepository
}
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")}
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 {
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"))
}
reqs := []requirements.Requirement{}
return reqs
}
func (cmd *Curl) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.curlRepo = deps.RepoLocator.GetCurlRepository()
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 := util.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, apiErr := cmd.curlRepo.Request(method, path, reqHeader, body)
if apiErr != nil {
return errors.New(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()}))
}
if trace.LoggingToStdout {
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)
}