browser.go 575 B

12345678910111213141516171819202122232425262728293031
  1. package utils
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "runtime"
  6. )
  7. // OpenBrowser opens the specified URL in the default browser of the user.
  8. func OpenBrowser(url string) error {
  9. var cmd string
  10. var args []string
  11. switch runtime.GOOS {
  12. case "windows":
  13. cmd = "cmd"
  14. args = []string{"/c", "start"}
  15. case "darwin":
  16. cmd = "open"
  17. default: // "linux", "freebsd", "openbsd", "netbsd"
  18. if CheckIfWsl() {
  19. cmd = "cmd.exe"
  20. args = []string{"/c", "start"}
  21. } else {
  22. cmd = "xdg-open"
  23. }
  24. }
  25. fmt.Println(url)
  26. args = append(args, url)
  27. return exec.Command(cmd, args...).Start()
  28. }