MCPcopy
hub / github.com/cloudfoundry/cli / Execute

Method Execute

cf/commands/curl.go:88–144  ·  view source on GitHub ↗
(c flags.FlagContext)

Source from the content-addressed store, hash-verified

86}
87
88func (cmd *Curl) Execute(c flags.FlagContext) error {
89 path := c.Args()[0]
90 headers := c.StringSlice("H")
91
92 var method string
93 var body string
94
95 if c.IsSet("d") {
96 method = "POST"
97
98 jsonBytes, err := flagcontext.GetContentsFromOptionalFlagValue(c.String("d"))
99 if err != nil {
100 return err
101 }
102 body = string(jsonBytes)
103 }
104
105 if c.IsSet("X") {
106 method = c.String("X")
107 }
108
109 reqHeader := strings.Join(headers, "\n")
110
111 responseHeader, responseBody, err := cmd.curlRepo.Request(method, path, reqHeader, body, c.Bool("fail"))
112 if err != nil {
113 if httpErr, ok := err.(cfErrors.HTTPError); ok && c.Bool("fail") {
114 return cfErrors.NewCurlHTTPError(httpErr.StatusCode())
115 }
116 return errors.New(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": err.Error()}))
117 }
118
119 if trace.LoggingToStdout && !cmd.pluginCall {
120 return nil
121 }
122
123 if c.Bool("i") {
124 cmd.ui.Say(responseHeader)
125 }
126
127 if c.String("output") != "" {
128 err := cmd.writeToFile(responseBody, c.String("output"))
129 if err != nil {
130 return errors.New(T("Error creating request:\n{{.Err}}", map[string]interface{}{"Err": err}))
131 }
132 } else {
133 if strings.Contains(responseHeader, "application/json") {
134 buffer := bytes.Buffer{}
135 err := json.Indent(&buffer, []byte(responseBody), "", " ")
136 if err == nil {
137 responseBody = buffer.String()
138 }
139 }
140
141 cmd.ui.Say(responseBody)
142 }
143 return nil
144}
145

Callers

nothing calls this directly

Calls 12

writeToFileMethod · 0.95
NewFunction · 0.92
ArgsMethod · 0.65
StringSliceMethod · 0.65
IsSetMethod · 0.65
StringMethod · 0.65
RequestMethod · 0.65
BoolMethod · 0.65
StatusCodeMethod · 0.65
ErrorMethod · 0.65
SayMethod · 0.65

Tested by

no test coverage detected