From dd84b9d64fb98746a230cd24233ff50a562c39c9 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 28 Apr 2023 01:36:44 +0800 Subject: --- cli/internal/util/browser/open.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cli/internal/util/browser/open.go (limited to 'cli/internal/util/browser/open.go') 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 +} -- cgit v1.2.3-70-g09d2