browser.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package browser provides utilities for interacting with users' browsers.
  5. package browser
  6. import (
  7. "os"
  8. "os/exec"
  9. "runtime"
  10. )
  11. // Commands returns a list of possible commands to use to open a url.
  12. func Commands() [][]string {
  13. var cmds [][]string
  14. if exe := os.Getenv("BROWSER"); exe != "" {
  15. cmds = append(cmds, []string{exe})
  16. }
  17. switch runtime.GOOS {
  18. case "darwin":
  19. cmds = append(cmds, []string{"/usr/bin/open"})
  20. case "windows":
  21. cmds = append(cmds, []string{"cmd", "/c", "start"})
  22. default:
  23. cmds = append(cmds, []string{"xdg-open"})
  24. }
  25. cmds = append(cmds,
  26. []string{"chrome"},
  27. []string{"google-chrome"},
  28. []string{"chromium"},
  29. []string{"firefox"},
  30. )
  31. return cmds
  32. }
  33. // Open tries to open url in a browser and reports whether it succeeded.
  34. func Open(url string) bool {
  35. for _, args := range Commands() {
  36. cmd := exec.Command(args[0], append(args[1:], url)...)
  37. if cmd.Start() == nil {
  38. return true
  39. }
  40. }
  41. return false
  42. }