Skip to content

Commit 4f78236

Browse files
authored
Suppress xdg-open warnings in auth login command (#3991)
## Changes Suppress `xdg-open` warnings when opening a browser for `auth login`. ## Why Before: ``` $ databricks auth login --profile myprofile /usr/bin/xdg-open: 869: www-browser: not found /usr/bin/xdg-open: 869: links2: not found /usr/bin/xdg-open: 869: elinks: not found /usr/bin/xdg-open: 869: links: not found /usr/bin/xdg-open: 869: lynx: not found /usr/bin/xdg-open: 869: w3m: not found xdg-open: no method available for opening 'https://...' Please open https://... in the browser to continue authenticationProfile myprofile was successfully saved ``` After (includes databricks/databricks-sdk-go#1334): ``` $ databricks auth login --profile myprofile Please continue the authentication process in your browser: https://... Profile myprofile was successfully saved ``` ## Tests Manual only.
1 parent bef542c commit 4f78236

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

cmd/auth/login.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78
"os"
89
"runtime"
910
"strings"
@@ -314,6 +315,22 @@ func loadProfileByName(ctx context.Context, profileName string, profiler profile
314315
return nil, nil
315316
}
316317

318+
// openURLSuppressingStderr opens a URL in the browser while suppressing stderr output.
319+
// This prevents xdg-open error messages from being displayed to the user.
320+
func openURLSuppressingStderr(url string) error {
321+
// Save the original stderr from the browser package
322+
originalStderr := browserpkg.Stderr
323+
defer func() {
324+
browserpkg.Stderr = originalStderr
325+
}()
326+
327+
// Redirect stderr to discard to suppress xdg-open errors
328+
browserpkg.Stderr = io.Discard
329+
330+
// Call the browser open function
331+
return browserpkg.OpenURL(url)
332+
}
333+
317334
// getBrowserFunc returns a function that opens the given URL in the browser.
318335
// It respects the BROWSER environment variable:
319336
// - empty string: uses the default browser
@@ -323,10 +340,10 @@ func getBrowserFunc(cmd *cobra.Command) func(url string) error {
323340
browser := env.Get(cmd.Context(), "BROWSER")
324341
switch browser {
325342
case "":
326-
return browserpkg.OpenURL
343+
return openURLSuppressingStderr
327344
case "none":
328345
return func(url string) error {
329-
fmt.Fprintf(cmd.OutOrStdout(), "Please open %s in the browser to continue authentication\n", url)
346+
fmt.Fprintf(cmd.OutOrStdout(), "Please complete authentication by opening this link in your browser:\n%s\n", url)
330347
return nil
331348
}
332349
default:

0 commit comments

Comments
 (0)