From 9c00ac0224b51166ced8b80211cfcee7888c83bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 21 Feb 2020 12:57:00 +0100 Subject: [PATCH] Tweak verbose HTTP logging - log headers only in DEBUG=api mode - enable color output on stderr - hide little-useful TLS debbuging info - ensure all request headers are logged --- api/client.go | 13 ++++++++----- command/root.go | 27 +++++++++++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/api/client.go b/api/client.go index 837fa81ca..a0ed6a859 100644 --- a/api/client.go +++ b/api/client.go @@ -38,12 +38,15 @@ func AddHeader(name, value string) ClientOption { } // VerboseLog enables request/response logging within a RoundTripper -func VerboseLog(out io.Writer, logBodies bool) ClientOption { +func VerboseLog(out io.Writer, logTraffic bool, colorize bool) ClientOption { logger := &httpretty.Logger{ - RequestHeader: true, - RequestBody: logBodies, - ResponseHeader: true, - ResponseBody: logBodies, + Time: true, + TLS: false, + Colors: colorize, + RequestHeader: logTraffic, + RequestBody: logTraffic, + ResponseHeader: logTraffic, + ResponseBody: logTraffic, Formatters: []httpretty.Formatter{&httpretty.JSONFormatter{}}, } logger.SetOutput(out) diff --git a/command/root.go b/command/root.go index 4f9d5ba09..0257b6a48 100644 --- a/command/root.go +++ b/command/root.go @@ -97,15 +97,14 @@ var initContext = func() context.Context { // BasicClient returns an API client that borrows from but does not depend on // user configuration func BasicClient() (*api.Client, error) { - opts := []api.ClientOption{ - api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)), + opts := []api.ClientOption{} + if verbose := os.Getenv("DEBUG"); verbose != "" { + opts = append(opts, apiVerboseLog()) } + opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version))) if c, err := context.ParseDefaultConfig(); err == nil { opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", c.Token))) } - if verbose := os.Getenv("DEBUG"); verbose != "" { - opts = append(opts, api.VerboseLog(os.Stderr, false)) - } return api.NewClient(opts...), nil } @@ -123,20 +122,28 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) { if err != nil { return nil, err } - opts := []api.ClientOption{ + opts := []api.ClientOption{} + if verbose := os.Getenv("DEBUG"); verbose != "" { + opts = append(opts, apiVerboseLog()) + } + opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", token)), api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)), // antiope-preview: Checks // shadow-cat-preview: Draft pull requests api.AddHeader("Accept", "application/vnd.github.antiope-preview+json, application/vnd.github.shadow-cat-preview"), api.AddHeader("GraphQL-Features", "pe_mobile"), - } - if verbose := os.Getenv("DEBUG"); verbose != "" { - opts = append(opts, api.VerboseLog(os.Stderr, strings.Contains(verbose, "api"))) - } + ) + return api.NewClient(opts...), nil } +func apiVerboseLog() api.ClientOption { + logTraffic := strings.Contains(os.Getenv("DEBUG"), "api") + colorize := utils.IsTerminal(os.Stderr) + return api.VerboseLog(utils.NewColorable(os.Stderr), logTraffic, colorize) +} + func colorableOut(cmd *cobra.Command) io.Writer { out := cmd.OutOrStdout() if outFile, isFile := out.(*os.File); isFile {