khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2015 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package client |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "io/ioutil" |
| 21 | "net/http" |
| 22 | "os" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | cURLDebug = false |
| 27 | ) |
| 28 | |
| 29 | func EnablecURLDebug() { |
| 30 | cURLDebug = true |
| 31 | } |
| 32 | |
| 33 | func DisablecURLDebug() { |
| 34 | cURLDebug = false |
| 35 | } |
| 36 | |
| 37 | // printcURL prints the cURL equivalent request to stderr. |
| 38 | // It returns an error if the body of the request cannot |
| 39 | // be read. |
| 40 | // The caller MUST cancel the request if there is an error. |
| 41 | func printcURL(req *http.Request) error { |
| 42 | if !cURLDebug { |
| 43 | return nil |
| 44 | } |
| 45 | var ( |
| 46 | command string |
| 47 | b []byte |
| 48 | err error |
| 49 | ) |
| 50 | |
| 51 | if req.URL != nil { |
| 52 | command = fmt.Sprintf("curl -X %s %s", req.Method, req.URL.String()) |
| 53 | } |
| 54 | |
| 55 | if req.Body != nil { |
| 56 | b, err = ioutil.ReadAll(req.Body) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | command += fmt.Sprintf(" -d %q", string(b)) |
| 61 | } |
| 62 | |
| 63 | fmt.Fprintf(os.Stderr, "cURL Command: %s\n", command) |
| 64 | |
| 65 | // reset body |
| 66 | body := bytes.NewBuffer(b) |
| 67 | req.Body = ioutil.NopCloser(body) |
| 68 | |
| 69 | return nil |
| 70 | } |