[cli] Helper method to open url in browser
This commit is contained in:
48
cli/utils/browser/browser.go
Normal file
48
cli/utils/browser/browser.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package browser
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// https://stackoverflow.com/questions/39320371/how-start-web-server-to-open-page-in-browser-in-golang
|
||||
// openURL opens the specified URL in the default browser of the user.
|
||||
func OpenURL(url string) error {
|
||||
var cmd string
|
||||
var args []string
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
cmd = "cmd"
|
||||
args = []string{"/c", "start"}
|
||||
case "darwin":
|
||||
cmd = "open"
|
||||
args = []string{url}
|
||||
default: // "linux", "freebsd", "openbsd", "netbsd"
|
||||
// Check if running under WSL
|
||||
if isWSL() {
|
||||
// Use 'cmd.exe /c start' to open the URL in the default Windows browser
|
||||
cmd = "cmd.exe"
|
||||
args = []string{"/c", "start", url}
|
||||
} else {
|
||||
// Use xdg-open on native Linux environments
|
||||
cmd = "xdg-open"
|
||||
args = []string{url}
|
||||
}
|
||||
}
|
||||
if len(args) > 1 {
|
||||
// args[0] is used for 'start' command argument, to prevent issues with URLs starting with a quote
|
||||
args = append(args[:1], append([]string{""}, args[1:]...)...)
|
||||
}
|
||||
return exec.Command(cmd, args...).Start()
|
||||
}
|
||||
|
||||
// isWSL checks if the Go program is running inside Windows Subsystem for Linux
|
||||
func isWSL() bool {
|
||||
releaseData, err := exec.Command("uname", "-r").Output()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(strings.ToLower(string(releaseData)), "microsoft")
|
||||
}
|
||||
Reference in New Issue
Block a user