This commit is contained in:
Michael Brewer 2026-02-03 17:54:30 +00:00 committed by GitHub
commit f1da367485
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View file

@ -85,6 +85,13 @@ func initCommands(
wd := WorkingDir(originalWorkingDir, os.Getenv("TF_DATA_DIR"))
var browserLauncher webbrowser.Launcher
if _, ok := os.LookupEnv("TF_BROWSER_ENV"); ok {
browserLauncher = webbrowser.NewBrowserEnvLauncher()
} else {
browserLauncher = webbrowser.NewNativeLauncher()
}
meta := command.Meta{
WorkingDir: wd,
Streams: streams,
@ -95,7 +102,7 @@ func initCommands(
Ui: Ui,
Services: services,
BrowserLauncher: webbrowser.NewNativeLauncher(),
BrowserLauncher: browserLauncher,
RunningInAutomation: inAutomation,
CLIConfigDir: configDir,

View file

@ -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)
}