mirror of
https://github.com/hashicorp/terraform.git
synced 2026-02-03 20:50:59 -05:00
Merge 353998097a into ba5c4ac5e3
This commit is contained in:
commit
f1da367485
2 changed files with 45 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
37
internal/command/webbrowser/browserenv.go
Normal file
37
internal/command/webbrowser/browserenv.go
Normal 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)
|
||||
}
|
||||
Loading…
Reference in a new issue