blob: c8bc9fba20ecc6120377f2cc1aed0bcdd35cc2d2 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// 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
15package client
16
17import (
18 "bytes"
19 "fmt"
20 "io/ioutil"
21 "net/http"
22 "os"
23)
24
25var (
26 cURLDebug = false
27)
28
29func EnablecURLDebug() {
30 cURLDebug = true
31}
32
33func 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.
41func 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}