forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(login): use BROWSER environment variable to open browser window
closes hashicorp#34902
- Loading branch information
1 parent
df738d4
commit 8571497
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package webbrowser | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/pkg/browser" | ||
) | ||
|
||
// NewBrowserEnvLauncher creates and returns a Launcher that will attempt to use | ||
// BROWSER environment , otherwise full back to the browser-launching mechanisms of | ||
// the operating system where the program is currently running. | ||
func NewBrowserEnvLauncher() Launcher { | ||
return browserEnvLauncher{} | ||
} | ||
|
||
type browserEnvLauncher struct{} | ||
|
||
func (l browserEnvLauncher) OpenURL(url string) error { | ||
browserEnv := os.Getenv("BROWSER") | ||
if browserEnv != "" { | ||
browserSh := fmt.Sprintf("%s '%s'", browserEnv, url) | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
cmd := exec.CommandContext(ctx, "sh", "-c", browserSh) | ||
_, err := cmd.CombinedOutput() | ||
return err | ||
} | ||
|
||
return browser.OpenURL(url) | ||
} |