From 07c5c0bb3e43b91442bc6b955aebcfff452ec523 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 19 Sep 2020 12:37:12 +0200 Subject: [PATCH 1/2] add debug mode --- gitea/client.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gitea/client.go b/gitea/client.go index b353e6e..f89caca 100644 --- a/gitea/client.go +++ b/gitea/client.go @@ -34,6 +34,7 @@ type Client struct { password string otp string sudo string + debug bool client *http.Client ctx context.Context serverVersion *version.Version @@ -135,7 +136,17 @@ func (c *Client) SetSudo(sudo string) { c.sudo = sudo } +// SetDebugMode is an option for NewClient to enable debug mode +func SetDebugMode() func(client *Client) { + return func(client *Client) { + client.debug = true + } +} + func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) { + if c.debug { + fmt.Printf("%s: %s\nBody: %v", method, path, body) + } req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body) if err != nil { return nil, nil, err @@ -147,10 +158,16 @@ func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *R defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) + if c.debug { + fmt.Printf("Response: %v", resp) + } return data, &Response{resp}, nil } func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) { + if c.debug { + fmt.Printf("%s: %s\nHeader: %v\nBody: %v", method, path, header, body) + } req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body) if err != nil { return nil, err @@ -175,6 +192,9 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read if err != nil { return nil, err } + if c.debug { + fmt.Printf("Response: %v", resp) + } return &Response{resp}, nil } -- 2.40.1 From 6ddb15d4ae93e359473507fc7c996994edebe70e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 19 Sep 2020 13:03:27 +0200 Subject: [PATCH 2/2] more info & better format --- gitea/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gitea/client.go b/gitea/client.go index f89caca..6513076 100644 --- a/gitea/client.go +++ b/gitea/client.go @@ -145,7 +145,7 @@ func SetDebugMode() func(client *Client) { func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *Response, error) { if c.debug { - fmt.Printf("%s: %s\nBody: %v", method, path, body) + fmt.Printf("%s: %s\nBody: %v\n", method, c.url+path, body) } req, err := http.NewRequestWithContext(c.ctx, method, c.url+path, body) if err != nil { @@ -159,14 +159,14 @@ func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, *R defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if c.debug { - fmt.Printf("Response: %v", resp) + fmt.Printf("Response: %v\n\n", resp) } return data, &Response{resp}, nil } func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*Response, error) { if c.debug { - fmt.Printf("%s: %s\nHeader: %v\nBody: %v", method, path, header, body) + fmt.Printf("%s: %s\nHeader: %v\nBody: %s\n", method, c.url+"/api/v1"+path, header, body) } req, err := http.NewRequestWithContext(c.ctx, method, c.url+"/api/v1"+path, body) if err != nil { @@ -193,7 +193,7 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read return nil, err } if c.debug { - fmt.Printf("Response: %v", resp) + fmt.Printf("Response: %v\n\n", resp) } return &Response{resp}, nil } -- 2.40.1