aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/util/browser/open.go
blob: a6171e93396b69390cc4bd8ae9ac5f743879ef80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package browser

import (
	"fmt"
	"os/exec"
	"runtime"
)

// OpenBrowser attempts to interactively open a browser window at the given URL
func OpenBrowser(url string) error {
	var err error

	switch runtime.GOOS {
	case "linux":
		if posixBinExists("wslview") {
			err = exec.Command("wslview", url).Start()
		} else {
			err = exec.Command("xdg-open", url).Start()
		}
	case "windows":
		err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
	case "darwin":
		err = exec.Command("open", url).Start()
	default:
		err = fmt.Errorf("unsupported platform")
	}
	if err != nil {
		return err
	}
	return nil
}

func posixBinExists(bin string) bool {
	err := exec.Command("which", bin).Run()
	// we mostly don't care what the error is, it suggests the binary is not usable
	return err == nil
}