Skip to content

Commit 19e2692

Browse files
committed
Merge pull request #141 from moul/debug-curl
Printing `curl` commands when debugging API requests
2 parents 29e9369 + 2812358 commit 19e2692

File tree

9 files changed

+179
-23
lines changed

9 files changed

+179
-23
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11281128

11291129
#### Features
11301130

1131+
* `-D,--debug` mode shows ready to copy-paste `curl` commands when using the API (must be used with `--sensitive` to unhide private token)
11311132
* Support of `_patch SERVER tags="tag1 tag2=value2 tag3"`
11321133
* `scw -D login` displays a fake password
11331134
* Support --skip-ssh-key `scw login` ([#129](https://github.com/scaleway/scaleway-cli/issues/129))

pkg/api/api.go

+43-20
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
2424
"github.com/scaleway/scaleway-cli/vendor/github.com/moul/anonuuid"
25+
"github.com/scaleway/scaleway-cli/vendor/github.com/moul/http2curl"
2526
)
2627

2728
// Default values
@@ -626,13 +627,21 @@ func (s *ScalewayAPI) Sync() {
626627
// GetResponse returns an http.Response object for the requested resource
627628
func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) {
628629
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
629-
log.Debugf("GET %s", uri)
630+
630631
req, err := http.NewRequest("GET", uri, nil)
631632
if err != nil {
632633
return nil, err
633634
}
634635
req.Header.Set("X-Auth-Token", s.Token)
635636
req.Header.Set("Content-Type", "application/json")
637+
638+
curl, err := http2curl.GetCurlCommand(req)
639+
if os.Getenv("SCW_SENSITIVE") != "1" {
640+
log.Debug(s.HideAPICredentials(curl.String()))
641+
} else {
642+
log.Debug(curl.String())
643+
}
644+
636645
return s.client.Do(req)
637646
}
638647

@@ -645,18 +654,20 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res
645654
return nil, err
646655
}
647656

648-
payloadString := strings.TrimSpace(fmt.Sprintf("%s", payload))
649-
if os.Getenv("SCW_SENSITIVE") != "1" {
650-
payloadString = s.HideAPICredentials(payloadString)
651-
}
652-
log.Debugf("POST %s payload=%s", uri, payloadString)
653-
654657
req, err := http.NewRequest("POST", uri, payload)
655658
if err != nil {
656659
return nil, err
657660
}
658661
req.Header.Set("X-Auth-Token", s.Token)
659662
req.Header.Set("Content-Type", "application/json")
663+
664+
curl, err := http2curl.GetCurlCommand(req)
665+
if os.Getenv("SCW_SENSITIVE") != "1" {
666+
log.Debug(s.HideAPICredentials(curl.String()))
667+
} else {
668+
log.Debug(curl.String())
669+
}
670+
660671
return s.client.Do(req)
661672
}
662673

@@ -669,18 +680,20 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re
669680
return nil, err
670681
}
671682

672-
payloadString := strings.TrimSpace(fmt.Sprintf("%s", payload))
673-
if os.Getenv("SCW_SENSITIVE") != "1" {
674-
payloadString = s.HideAPICredentials(payloadString)
675-
}
676-
log.Debugf("PATCH %s payload=%s", uri, payloadString)
677-
678683
req, err := http.NewRequest("PATCH", uri, payload)
679684
if err != nil {
680685
return nil, err
681686
}
682687
req.Header.Set("X-Auth-Token", s.Token)
683688
req.Header.Set("Content-Type", "application/json")
689+
690+
curl, err := http2curl.GetCurlCommand(req)
691+
if os.Getenv("SCW_SENSITIVE") != "1" {
692+
log.Debug(s.HideAPICredentials(curl.String()))
693+
} else {
694+
log.Debug(curl.String())
695+
}
696+
684697
return s.client.Do(req)
685698
}
686699

@@ -693,31 +706,41 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp
693706
return nil, err
694707
}
695708

696-
payloadString := strings.TrimSpace(fmt.Sprintf("%s", payload))
697-
if os.Getenv("SCW_SENSITIVE") != "1" {
698-
payloadString = s.HideAPICredentials(payloadString)
699-
}
700-
log.Debugf("PUT %s payload=%s", uri, payloadString)
701-
702709
req, err := http.NewRequest("PUT", uri, payload)
703710
if err != nil {
704711
return nil, err
705712
}
706713
req.Header.Set("X-Auth-Token", s.Token)
707714
req.Header.Set("Content-Type", "application/json")
715+
716+
curl, err := http2curl.GetCurlCommand(req)
717+
if os.Getenv("SCW_SENSITIVE") != "1" {
718+
log.Debug(s.HideAPICredentials(curl.String()))
719+
} else {
720+
log.Debug(curl.String())
721+
}
722+
708723
return s.client.Do(req)
709724
}
710725

711726
// DeleteResponse returns an http.Response object for the deleted resource
712727
func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) {
713728
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
714-
log.Debugf("DELETE %s", uri)
729+
715730
req, err := http.NewRequest("DELETE", uri, nil)
716731
if err != nil {
717732
return nil, err
718733
}
719734
req.Header.Set("X-Auth-Token", s.Token)
720735
req.Header.Set("Content-Type", "application/json")
736+
737+
curl, err := http2curl.GetCurlCommand(req)
738+
if os.Getenv("SCW_SENSITIVE") != "1" {
739+
log.Debug(s.HideAPICredentials(curl.String()))
740+
} else {
741+
log.Debug(curl.String())
742+
}
743+
721744
return s.client.Do(req)
722745
}
723746

pkg/cli/x_patch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func runPatch(cmd *Command, args []string) error {
104104
log.Debugf("no changes, not updating server")
105105
}
106106
if err != nil {
107-
log.Fatalf("Cannot rename server: %v", err)
107+
log.Fatalf("Cannot update server: %v", err)
108108
}
109109
default:
110110
log.Fatalf("_patch not implemented for this kind of object")

pkg/sshcommand/sshcommand.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ func (c *Command) Slice() []string {
107107
func (c *Command) String() string {
108108
slice := c.Slice()
109109
for i := range slice {
110-
if strings.Contains(slice[i], " ") {
111-
slice[i] = fmt.Sprintf("%q", slice[i])
110+
quoted := fmt.Sprintf("%q", slice[i])
111+
if strings.Contains(slice[i], " ") || len(quoted) != len(slice[i])+2 {
112+
slice[i] = quoted
112113
}
113114
}
114115
return strings.Join(slice, " ")

vendor/github.com/moul/http2curl/LICENSE

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moul/http2curl/Makefile

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moul/http2curl/README.md

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moul/http2curl/http2curl.go

+66
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moul/http2curl/http2curl_test.go

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)