Support GH_DEBUG to control verbosity, deprecate DEBUG (#5306) · cli/cli@56fda0f · GitHub
Skip to content

Commit 56fda0f

Browse files
LyleJackmislav
andauthored
Support GH_DEBUG to control verbosity, deprecate DEBUG (#5306)
The GH_DEBUG environment variable is a new gh-specific verbosity control. For backwards-compatibility, DEBUG will still be respected if it has values "1", "true", "yes", and "api", but any other values will be ignored. Finally, support for "oauth" debug value has been dropped in favor of "api". The "oauth" value only had limited, internal use. Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent c1e5934 commit 56fda0f

7 files changed

Lines changed: 85 additions & 20 deletions

File tree

‎cmd/gh/main.go‎

Lines changed: 5 additions & 9 deletions

‎internal/authflow/flow.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cli/cli/v2/internal/ghinstance"
1313
"github.com/cli/cli/v2/pkg/cmdutil"
1414
"github.com/cli/cli/v2/pkg/iostreams"
15+
"github.com/cli/cli/v2/utils"
1516
"github.com/cli/oauth"
1617
)
1718

@@ -53,8 +54,9 @@ func authFlow(oauthHost string, IO *iostreams.IOStreams, notice string, addition
5354
cs := IO.ColorScheme()
5455

5556
httpClient := http.DefaultClient
56-
if envDebug := os.Getenv("DEBUG"); envDebug != "" {
57-
logTraffic := strings.Contains(envDebug, "api") || strings.Contains(envDebug, "oauth")
57+
debugEnabled, debugValue := utils.IsDebugEnabled()
58+
if debugEnabled {
59+
logTraffic := strings.Contains(debugValue, "api")
5860
httpClient.Transport = api.VerboseLog(IO.ErrOut, logTraffic, IO.ColorEnabled())(httpClient.Transport)
5961
}
6062

‎internal/run/run.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"strings"
11+
12+
"github.com/cli/cli/v2/utils"
1113
)
1214

1315
// Runnable is typically an exec.Cmd or its stub in tests
@@ -28,7 +30,7 @@ type cmdWithStderr struct {
2830
}
2931

3032
func (c cmdWithStderr) Output() ([]byte, error) {
31-
if os.Getenv("DEBUG") != "" {
33+
if isVerbose, _ := utils.IsDebugEnabled(); isVerbose {
3234
_ = printArgs(os.Stderr, c.Cmd.Args)
3335
}
3436
if c.Cmd.Stderr != nil {
@@ -44,7 +46,7 @@ func (c cmdWithStderr) Output() ([]byte, error) {
4446
}
4547

4648
func (c cmdWithStderr) Run() error {
47-
if os.Getenv("DEBUG") != "" {
49+
if isVerbose, _ := utils.IsDebugEnabled(); isVerbose {
4850
_ = printArgs(os.Stderr, c.Cmd.Args)
4951
}
5052
if c.Cmd.Stderr != nil {

‎pkg/cmd/factory/http.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package factory
33
import (
44
"fmt"
55
"net/http"
6-
"os"
76
"regexp"
87
"strings"
98
"time"
@@ -12,6 +11,7 @@ import (
1211
"github.com/cli/cli/v2/internal/ghinstance"
1312
"github.com/cli/cli/v2/internal/httpunix"
1413
"github.com/cli/cli/v2/pkg/iostreams"
14+
"github.com/cli/cli/v2/utils"
1515
)
1616

1717
var timezoneNames = map[int]string{
@@ -84,8 +84,8 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg configGetter, appVersion string,
8484
}))
8585
}
8686

87-
if verbose := os.Getenv("DEBUG"); verbose != "" {
88-
logTraffic := strings.Contains(verbose, "api")
87+
if isVerbose, debugValue := utils.IsDebugEnabled(); isVerbose {
88+
logTraffic := strings.Contains(debugValue, "api")
8989
opts = append(opts, api.VerboseLog(io.ErrOut, logTraffic, io.IsStderrTTY()))
9090
}
9191

‎pkg/cmd/factory/http_test.go‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func TestNewHTTPClient(t *testing.T) {
2424
name string
2525
args args
2626
envDebug string
27+
setGhDebug bool
28+
envGhDebug string
2729
host string
2830
sso string
2931
wantHeader map[string]string
@@ -82,8 +84,39 @@ func TestNewHTTPClient(t *testing.T) {
8284
appVersion: "v1.2.3",
8385
setAccept: true,
8486
},
85-
host: "github.com",
86-
envDebug: "api",
87+
host: "github.com",
88+
envDebug: "api",
89+
setGhDebug: false,
90+
wantHeader: map[string]string{
91+
"authorization": "token MYTOKEN",
92+
"user-agent": "GitHub CLI v1.2.3",
93+
"accept": "application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview",
94+
},
95+
wantStderr: heredoc.Doc(`
96+
* Request at <time>
97+
* Request to http://<host>:<port>
98+
> GET / HTTP/1.1
99+
> Host: github.com
100+
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
101+
> Authorization: token ████████████████████
102+
> User-Agent: GitHub CLI v1.2.3
103+
104+
< HTTP/1.1 204 No Content
105+
< Date: <time>
106+
107+
* Request took <duration>
108+
`),
109+
},
110+
{
111+
name: "github.com in verbose mode",
112+
args: args{
113+
config: tinyConfig{"github.com:oauth_token": "MYTOKEN"},
114+
appVersion: "v1.2.3",
115+
setAccept: true,
116+
},
117+
host: "github.com",
118+
envGhDebug: "api",
119+
setGhDebug: true,
87120
wantHeader: map[string]string{
88121
"authorization": "token MYTOKEN",
89122
"user-agent": "GitHub CLI v1.2.3",
@@ -145,9 +178,16 @@ func TestNewHTTPClient(t *testing.T) {
145178
for _, tt := range tests {
146179
t.Run(tt.name, func(t *testing.T) {
147180
oldDebug := os.Getenv("DEBUG")
181+
oldGhDebug := os.Getenv("GH_DEBUG")
148182
os.Setenv("DEBUG", tt.envDebug)
183+
if tt.setGhDebug {
184+
os.Setenv("GH_DEBUG", tt.envGhDebug)
185+
} else {
186+
os.Unsetenv("GH_DEBUG")
187+
}
149188
t.Cleanup(func() {
150189
os.Setenv("DEBUG", oldDebug)
190+
os.Setenv("GH_DEBUG", oldGhDebug)
151191
})
152192

153193
io, _, _, stderr := iostreams.Test()

‎pkg/cmd/root/help_topic.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ var HelpTopics = map[string]map[string]string{
4747
4848
GH_BROWSER, BROWSER (in order of precedence): the web browser to use for opening links.
4949
50-
DEBUG: set to any value to enable verbose output to standard error. Include values "api"
51-
or "oauth" to print detailed information about HTTP requests or authentication flow.
50+
GH_DEBUG: set to a truthy value to enable verbose output on standard error. Set to "api"
51+
to additionally log details of HTTP traffic.
52+
53+
DEBUG (deprecated): set to "1", "true", or "yes" to enable verbose output on standard
54+
error.
5255
5356
GH_PAGER, PAGER (in order of precedence): a terminal paging program to send standard output
5457
to, e.g. "less".

‎utils/utils.go‎

Lines changed: 22 additions & 0 deletions

0 commit comments

Comments
 (0)