From a88f9592d89c7702ed45ca43a6d974619706a418 Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Wed, 20 Jan 2021 18:59:59 -0800 Subject: [PATCH] Use Edge on Windows if Chrome not installed --- win/main.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/win/main.go b/win/main.go index 742ad8ce3..87a2cb136 100644 --- a/win/main.go +++ b/win/main.go @@ -5,6 +5,7 @@ import ( "log" "os" "os/exec" + "runtime" "github.com/gonutz/w32" "github.com/zserge/lorca" @@ -36,6 +37,11 @@ func main() { ctx, cancel := context.WithCancel(context.Background()) cmd := runOctoTasks(ctx) + chromePath := locateChrome() + if len(chromePath) > 0 { + os.Setenv("LORCACHROME", chromePath) + } + ui, err := lorca.New("http://localhost:8088", "", 1024, 768) if err != nil { log.Fatal(err) @@ -61,3 +67,54 @@ func hideConsole() { } } } + +// This duplicates the logic in Lorca, but adds Edge as an option for Windows, fallback to standard logic for other OSes +func locateChrome() string { + // If env variable "LORCACHROME" specified and it exists + if path, ok := os.LookupEnv("LORCACHROME"); ok { + if _, err := os.Stat(path); err == nil { + return path + } + } + + var paths []string + switch runtime.GOOS { + // case "darwin": + // paths = []string{ + // "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", + // "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary", + // "/Applications/Chromium.app/Contents/MacOS/Chromium", + // "/usr/bin/google-chrome-stable", + // "/usr/bin/google-chrome", + // "/usr/bin/chromium", + // "/usr/bin/chromium-browser", + // } + case "windows": + paths = []string{ + os.Getenv("LocalAppData") + "/Google/Chrome/Application/chrome.exe", + os.Getenv("ProgramFiles") + "/Google/Chrome/Application/chrome.exe", + os.Getenv("ProgramFiles(x86)") + "/Google/Chrome/Application/chrome.exe", + os.Getenv("LocalAppData") + "/Chromium/Application/chrome.exe", + os.Getenv("ProgramFiles") + "/Chromium/Application/chrome.exe", + os.Getenv("ProgramFiles(x86)") + "/Chromium/Application/chrome.exe", + os.Getenv("ProgramFiles(x86)") + "/Microsoft/Edge/Application/msedge.exe", + } + // default: + // paths = []string{ + // "/usr/bin/google-chrome-stable", + // "/usr/bin/google-chrome", + // "/usr/bin/chromium", + // "/usr/bin/chromium-browser", + // "/snap/bin/chromium", + // } + } + + for _, path := range paths { + if _, err := os.Stat(path); os.IsNotExist(err) { + continue + } + return path + } + + return "" +}