Skip to content

Commit 1ebc26a

Browse files
committed
New option -auto to use device authorization depending on environment variable XDG_SESSION_TYPE
1 parent 69dda76 commit 1ebc26a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ func parse(input string) map[string]string {
149149

150150
func main() {
151151
ctx := context.Background()
152-
flag.BoolVar(&verbose, "verbose", false, "log debug information to stderr")
152+
flag.BoolVar(&verbose, "verbose", false, "Log debug information to standard error stream")
153153
var device bool
154-
flag.BoolVar(&device, "device", false, "instead of opening a web browser locally, print a code to enter on another device")
154+
flag.BoolVar(&device, "device", false, "Instead of opening a web browser locally, print a code to enter on another device. OAuth device authorization is useful for systems without a web browser.")
155+
var auto bool
156+
flag.BoolVar(&auto, "auto", false, "Automatically use device authorization when environment variable XDG_SESSION_TYPE has value 'tty'")
155157
var bearer bool
156158
flag.BoolVar(&bearer, "bearer", false, "Prefer Bearer authentication for supported hosts")
157159
flag.Usage = func() {
@@ -308,7 +310,7 @@ func main() {
308310

309311
if token == nil {
310312
// Generate new token (opens browser, may require user input)
311-
if device {
313+
if device || (auto && os.Getenv("XDG_SESSION_TYPE") == "tty") {
312314
token, err = getDeviceToken(ctx, c)
313315
} else {
314316
token, err = getToken(ctx, c, authURLSuffix)
@@ -369,9 +371,12 @@ func main() {
369371
// six hours
370372
storage = "cache --timeout 21600"
371373
}
372-
commands = []*exec.Cmd{exec.Command(gitPath, "config", "--global", "--unset-all", "credential.helper"),
374+
commands = []*exec.Cmd{
375+
exec.Command(gitPath, "config", "--global", "--unset-all", "credential.helper"),
373376
exec.Command(gitPath, "config", "--global", "--add", "credential.helper", storage),
374-
exec.Command(gitPath, "config", "--global", "--add", "credential.helper", "oauth")}
377+
exec.Command(gitPath, "config", "--global", "--add", "credential.helper", "oauth -auto"),
378+
exec.Command(gitPath, "config", "--global", "--unset-all", "credential.useHttpPath"),
379+
}
375380
} else if args[0] == "unconfigure" {
376381
commands = []*exec.Cmd{exec.Command(gitPath, "config", "--global", "--unset-all", "credential.helper", "oauth")}
377382
}

0 commit comments

Comments
 (0)