Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Commit 5d33b92

Browse files
committed
Use pkg/browser where possible and remove explicit firefox support
Closes #13 and closes #10
1 parent b98aa3f commit 5d33b92

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module go.coder.com/sshcode
33
go 1.12
44

55
require (
6+
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
67
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8
78
golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be // indirect
89
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU
44
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
55
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
66
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
7+
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
8+
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
79
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8 h1:PtQ3moPi4EAz3cyQhkUs1IGIXa2QgJpP60yMjOdu0kk=
810
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8/go.mod h1:83JsYgXYv0EOaXjIMnaZ1Fl6ddNB3fJnDZ/8845mUJ8=
911
golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be h1:mI+jhqkn68ybP0ORJqunXn+fq+Eeb4hHKqLQcFICjAc=

main.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"github.com/pkg/browser"
78
"math/rand"
89
"net"
910
"net/http"
@@ -139,29 +140,37 @@ func openBrowser(url string) {
139140
var openCmd *exec.Cmd
140141
switch {
141142
case commandExists("google-chrome"):
142-
openCmd = exec.Command("google-chrome", fmtChromeOptions(url)...)
143+
openCmd = exec.Command("google-chrome", chromeOptions(url)...)
143144
case commandExists("chromium"):
144-
openCmd = exec.Command("chromium", fmtChromeOptions(url)...)
145+
openCmd = exec.Command("chromium", chromeOptions(url)...)
145146
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)...)
149148
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)...)
151150
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
153156
}
154157

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.
155160
err := openCmd.Start()
156161
if err != nil {
157-
flog.Fatal("failed to open browser: %v", err)
162+
flog.Error("failed to open browser: %v", err)
158163
}
159164
}
160165

161-
func fmtChromeOptions(url string) []string {
166+
func chromeOptions(url string) []string {
162167
return []string{"--app=" + url, "--disable-extensions", "--disable-plugins"}
163168
}
164169

170+
func firefoxOptions(url string) []string {
171+
return []string{"--url="+url, "-safe-mode"}
172+
}
173+
165174
// Checks if a command exists locally.
166175
func commandExists(name string) bool {
167176
_, err := exec.LookPath(name)

0 commit comments

Comments
 (0)