From 12e090ce48f17e970cd7ba5de129f638ae296303 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Thu, 13 Feb 2020 14:30:46 -0500 Subject: [PATCH] command/login: Fix browser launcher for WSL users With the current implementation of terraform login, Windows Subsystem for Linux fails to open a browser due to lack of support for xdg-open. This commit reuses a fix from pkg/browser#8 which detects a WSL environment and uses cmd.exe to open the URL instead. --- command/webbrowser/native.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/command/webbrowser/native.go b/command/webbrowser/native.go index 4e8281ce1..77d503a2c 100644 --- a/command/webbrowser/native.go +++ b/command/webbrowser/native.go @@ -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) }