Merge pull request #24107 from hashicorp/alisdair/terraform-login-wsl-browser-launcher-fix

command/login: Fix browser launcher for WSL users
This commit is contained in:
Alisdair McDiarmid 2020-02-14 16:32:35 -05:00 committed by GitHub
commit 7696c14661
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package webbrowser
import (
"github.com/pkg/browser"
"os/exec"
"strings"
)
// NewNativeLauncher creates and returns a Launcher that will attempt to interact
@ -13,6 +15,18 @@ func NewNativeLauncher() Launcher {
type nativeLauncher struct{}
func hasProgram(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}
func (l nativeLauncher) OpenURL(url string) error {
// Windows Subsystem for Linux (bash for Windows) doesn't have xdg-open available
// but you can execute cmd.exe from there; try to identify it
if !hasProgram("xdg-open") && hasProgram("cmd.exe") {
r := strings.NewReplacer("&", "^&")
exec.Command("cmd.exe", "/c", "start", r.Replace(url)).Run()
}
return browser.OpenURL(url)
}