|
4 | 4 | "context"
|
5 | 5 | "flag"
|
6 | 6 | "fmt"
|
| 7 | + "github.com/pkg/browser" |
7 | 8 | "math/rand"
|
8 | 9 | "net"
|
9 | 10 | "net/http"
|
@@ -139,29 +140,37 @@ func openBrowser(url string) {
|
139 | 140 | var openCmd *exec.Cmd
|
140 | 141 | switch {
|
141 | 142 | case commandExists("google-chrome"):
|
142 |
| - openCmd = exec.Command("google-chrome", fmtChromeOptions(url)...) |
| 143 | + openCmd = exec.Command("google-chrome", chromeOptions(url)...) |
143 | 144 | case commandExists("chromium"):
|
144 |
| - openCmd = exec.Command("chromium", fmtChromeOptions(url)...) |
| 145 | + openCmd = exec.Command("chromium", chromeOptions(url)...) |
145 | 146 | case commandExists("chromium-browser"):
|
146 |
| - openCmd = exec.Command("chromium-browser", fmtChromeOptions(url)...) |
147 |
| - case commandExists("firefox"): |
148 |
| - openCmd = exec.Command("firefox", "--url="+url, "-safe-mode") |
| 147 | + openCmd = exec.Command("chromium-browser", chromeOptions(url)...) |
149 | 148 | case pathExists("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"):
|
150 |
| - openCmd = exec.Command("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", fmtChromeOptions(url)...) |
| 149 | + openCmd = exec.Command("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", chromeOptions(url)...) |
151 | 150 | default:
|
152 |
| - flog.Info("unable to find a browser to open: sshcode only supports firefox, chrome, and chromium") |
| 151 | + err := browser.OpenURL(url) |
| 152 | + if err != nil { |
| 153 | + flog.Error("failed to open browser: %v", err) |
| 154 | + } |
| 155 | + return |
153 | 156 | }
|
154 | 157 |
|
| 158 | + // We do not use CombinedOutput because if there is no chrome instance, this will block |
| 159 | + // and become the parent process instead of using an existing chrome instance. |
155 | 160 | err := openCmd.Start()
|
156 | 161 | if err != nil {
|
157 |
| - flog.Fatal("failed to open browser: %v", err) |
| 162 | + flog.Error("failed to open browser: %v", err) |
158 | 163 | }
|
159 | 164 | }
|
160 | 165 |
|
161 |
| -func fmtChromeOptions(url string) []string { |
| 166 | +func chromeOptions(url string) []string { |
162 | 167 | return []string{"--app=" + url, "--disable-extensions", "--disable-plugins"}
|
163 | 168 | }
|
164 | 169 |
|
| 170 | +func firefoxOptions(url string) []string { |
| 171 | + return []string{"--url="+url, "-safe-mode"} |
| 172 | +} |
| 173 | + |
165 | 174 | // Checks if a command exists locally.
|
166 | 175 | func commandExists(name string) bool {
|
167 | 176 | _, err := exec.LookPath(name)
|
|
0 commit comments