From 8a63f79803583ae61e018a29a5583409d5333b9a Mon Sep 17 00:00:00 2001 From: Juha Pohjalainen Date: Mon, 23 Jan 2023 11:28:39 +0200 Subject: [PATCH] IMPROVEMENT: netdiagnostics trace change (v13.1.2) - improvement: netdiagnostics with `--trace` flag will now list response header information --- cloud/client.go | 20 ++++++++++++++++++++ common/version.go | 2 +- docs/changelog.md | 5 +++++ mocks/client.go | 4 ++++ operations/netdiagnostics.go | 6 ++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cloud/client.go b/cloud/client.go index 86236e47..3b4718b8 100644 --- a/cloud/client.go +++ b/cloud/client.go @@ -13,6 +13,7 @@ import ( "github.com/robocorp/rcc/common" "github.com/robocorp/rcc/pathlib" + "github.com/robocorp/rcc/set" "github.com/robocorp/rcc/settings" "github.com/robocorp/rcc/xviper" ) @@ -20,6 +21,7 @@ import ( type internalClient struct { endpoint string client *http.Client + tracing bool } type Request struct { @@ -48,6 +50,7 @@ type Client interface { Delete(request *Request) *Response NewClient(endpoint string) (Client, error) WithTimeout(time.Duration) Client + WithTracing() Client } func EnsureHttps(endpoint string) (string, error) { @@ -73,6 +76,7 @@ func NewClient(endpoint string) (Client, error) { return &internalClient{ endpoint: https, client: &http.Client{Transport: settings.Global.ConfiguredHttpTransport()}, + tracing: false, }, nil } @@ -83,6 +87,15 @@ func (it *internalClient) WithTimeout(timeout time.Duration) Client { Transport: settings.Global.ConfiguredHttpTransport(), Timeout: timeout, }, + tracing: it.tracing, + } +} + +func (it *internalClient) WithTracing() Client { + return &internalClient{ + endpoint: it.endpoint, + client: it.client, + tracing: true, } } @@ -128,6 +141,13 @@ func (it *internalClient) does(method string, request *Request) *Response { return response } defer httpResponse.Body.Close() + if it.tracing { + common.Trace("Response %d headers:", httpResponse.StatusCode) + keys := set.Keys(httpResponse.Header) + for _, key := range keys { + common.Trace("> %s: %q", key, httpResponse.Header[key]) + } + } response.Status = httpResponse.StatusCode if request.Stream != nil { io.Copy(request.Stream, httpResponse.Body) diff --git a/common/version.go b/common/version.go index 732692b2..e8e5238a 100644 --- a/common/version.go +++ b/common/version.go @@ -1,5 +1,5 @@ package common const ( - Version = `v13.1.1` + Version = `v13.1.2` ) diff --git a/docs/changelog.md b/docs/changelog.md index 0d7acc0c..e88c274f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,10 @@ # rcc change log +## v13.1.2 (date: 23.1.2023) + +- improvement: netdiagnostics with `--trace` flag will now list response + header information + ## v13.1.1 (date: 20.1.2023) UNSTABLE - fix: netdiagnostics configuration flag change (now it is `--checks filename`) diff --git a/mocks/client.go b/mocks/client.go index b1431a15..4739d26c 100644 --- a/mocks/client.go +++ b/mocks/client.go @@ -33,6 +33,10 @@ func (it *MockClient) WithTimeout(time.Duration) cloud.Client { return it } +func (it *MockClient) WithTracing() cloud.Client { + return it +} + func (it *MockClient) NewRequest(url string) *cloud.Request { return &cloud.Request{ Url: url, diff --git a/operations/netdiagnostics.go b/operations/netdiagnostics.go index a0043bbf..c73cfea4 100644 --- a/operations/netdiagnostics.go +++ b/operations/netdiagnostics.go @@ -141,6 +141,9 @@ func headRequest(link string) (code int, fingerprint string, err error) { client, err := cloud.NewClient(link) fail.On(err != nil, "Client for %q failed, reason: %v", link, err) + if common.TraceFlag { + client = client.WithTracing() + } request := client.NewRequest("") response := client.Head(request) fail.On(response.Err != nil, "HEAD request to %q failed, reason: %v", link, response.Err) @@ -153,6 +156,9 @@ func getRequest(link string) (code int, fingerprint string, err error) { client, err := cloud.NewClient(link) fail.On(err != nil, "Client for %q failed, reason: %v", link, err) + if common.TraceFlag { + client = client.WithTracing() + } request := client.NewRequest("") response := client.Get(request) fail.On(response.Err != nil, "HEAD request to %q failed, reason: %v", link, response.Err)