github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/command/webbrowser/native.go (about)

     1  package webbrowser
     2  
     3  import (
     4  	"github.com/pkg/browser"
     5  	"os/exec"
     6  	"strings"
     7  )
     8  
     9  // NewNativeLauncher creates and returns a Launcher that will attempt to interact
    10  // with the browser-launching mechanisms of the operating system where the
    11  // program is currently running.
    12  func NewNativeLauncher() Launcher {
    13  	return nativeLauncher{}
    14  }
    15  
    16  type nativeLauncher struct{}
    17  
    18  func hasProgram(name string) bool {
    19  	_, err := exec.LookPath(name)
    20  	return err == nil
    21  }
    22  
    23  func (l nativeLauncher) OpenURL(url string) error {
    24  	// Windows Subsystem for Linux (bash for Windows) doesn't have xdg-open available
    25  	// but you can execute cmd.exe from there; try to identify it
    26  	if !hasProgram("xdg-open") && hasProgram("cmd.exe") {
    27  		r := strings.NewReplacer("&", "^&")
    28  		exec.Command("cmd.exe", "/c", "start", r.Replace(url)).Run()
    29  	}
    30  
    31  	return browser.OpenURL(url)
    32  }