aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/util/browser
diff options
context:
space:
mode:
Diffstat (limited to 'cli/internal/util/browser')
-rw-r--r--cli/internal/util/browser/open.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/cli/internal/util/browser/open.go b/cli/internal/util/browser/open.go
new file mode 100644
index 0000000..a6171e9
--- /dev/null
+++ b/cli/internal/util/browser/open.go
@@ -0,0 +1,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
+}