locate.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package lorca
  2. import (
  3. "os"
  4. "os/exec"
  5. "runtime"
  6. "strings"
  7. )
  8. // ChromeExecutable returns a string which points to the preferred Chrome
  9. // executable file.
  10. var ChromeExecutable = LocateChrome
  11. // LocateChrome returns a path to the Chrome binary, or an empty string if
  12. // Chrome installation is not found.
  13. func LocateChrome() string {
  14. // If env variable "LORCACHROME" specified and it exists
  15. if path, ok := os.LookupEnv("LORCACHROME"); ok {
  16. if _, err := os.Stat(path); err == nil {
  17. return path
  18. }
  19. }
  20. var paths []string
  21. switch runtime.GOOS {
  22. case "darwin":
  23. paths = []string{
  24. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
  25. "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
  26. "/Applications/Chromium.app/Contents/MacOS/Chromium",
  27. "/usr/bin/google-chrome-stable",
  28. "/usr/bin/google-chrome",
  29. "/usr/bin/chromium",
  30. "/usr/bin/chromium-browser",
  31. }
  32. case "windows":
  33. paths = []string{
  34. os.Getenv("LocalAppData") + "/Google/Chrome/Application/chrome.exe",
  35. os.Getenv("ProgramFiles") + "/Google/Chrome/Application/chrome.exe",
  36. os.Getenv("ProgramFiles(x86)") + "/Google/Chrome/Application/chrome.exe",
  37. os.Getenv("LocalAppData") + "/Chromium/Application/chrome.exe",
  38. os.Getenv("ProgramFiles") + "/Chromium/Application/chrome.exe",
  39. os.Getenv("ProgramFiles(x86)") + "/Chromium/Application/chrome.exe",
  40. os.Getenv("ProgramFiles(x86)") + "/Microsoft/Edge/Application/msedge.exe",
  41. }
  42. default:
  43. paths = []string{
  44. "/usr/bin/google-chrome-stable",
  45. "/usr/bin/google-chrome",
  46. "/usr/bin/chromium",
  47. "/usr/bin/chromium-browser",
  48. "/snap/bin/chromium",
  49. }
  50. }
  51. for _, path := range paths {
  52. if _, err := os.Stat(path); os.IsNotExist(err) {
  53. continue
  54. }
  55. return path
  56. }
  57. return ""
  58. }
  59. // PromptDownload asks user if he wants to download and install Chrome, and
  60. // opens a download web page if the user agrees.
  61. func PromptDownload() {
  62. title := "Chrome not found"
  63. text := "No Chrome/Chromium installation was found. Would you like to download and install it now?"
  64. // Ask user for confirmation
  65. if !messageBox(title, text) {
  66. return
  67. }
  68. // Open download page
  69. url := "https://www.google.com/chrome/"
  70. switch runtime.GOOS {
  71. case "linux":
  72. exec.Command("xdg-open", url).Run()
  73. case "darwin":
  74. exec.Command("open", url).Run()
  75. case "windows":
  76. r := strings.NewReplacer("&", "^&")
  77. exec.Command("cmd", "/c", "start", r.Replace(url)).Run()
  78. }
  79. }