2
0

browser.go 660 B

123456789101112131415161718192021222324252627282930313233
  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. fmt.Printf("Attempting to open your browser. If this does not work, please navigate to: %s\n", url)
  12. switch runtime.GOOS {
  13. case "windows":
  14. cmd = "cmd"
  15. args = []string{"/c", "start"}
  16. case "darwin":
  17. cmd = "open"
  18. default: // "linux", "freebsd", "openbsd", "netbsd"
  19. if CheckIfWsl() {
  20. cmd = "cmd.exe"
  21. args = []string{"/c", "start"}
  22. } else {
  23. cmd = "xdg-open"
  24. }
  25. }
  26. args = append(args, url)
  27. return exec.Command(cmd, args...).Start()
  28. }