From 5902ee0b388963242b3d04feb1f7f90709222148 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 19 Apr 2019 21:19:28 -0400 Subject: [PATCH 1/2] Use pkg/browser where possible and remove explicit firefox support Closes #13 and closes #10 --- go.mod | 1 + go.sum | 2 ++ main.go | 30 +++++++++++++++++++----------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 5548305..c5ea990 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module go.coder.com/sshcode go 1.12 require ( + github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8 golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be // indirect golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373 diff --git a/go.sum b/go.sum index 527a8dd..804a9d7 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98= +github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA= go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8 h1:PtQ3moPi4EAz3cyQhkUs1IGIXa2QgJpP60yMjOdu0kk= go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8/go.mod h1:83JsYgXYv0EOaXjIMnaZ1Fl6ddNB3fJnDZ/8845mUJ8= golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be h1:mI+jhqkn68ybP0ORJqunXn+fq+Eeb4hHKqLQcFICjAc= diff --git a/main.go b/main.go index cf0d82b..b0f84f1 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,9 @@ import ( "strconv" "time" - "golang.org/x/xerrors" - + "github.com/pkg/browser" "go.coder.com/flog" + "golang.org/x/xerrors" ) func init() { @@ -139,29 +139,37 @@ func openBrowser(url string) { var openCmd *exec.Cmd switch { case commandExists("google-chrome"): - openCmd = exec.Command("google-chrome", fmtChromeOptions(url)...) + openCmd = exec.Command("google-chrome", chromeOptions(url)...) case commandExists("chromium"): - openCmd = exec.Command("chromium", fmtChromeOptions(url)...) + openCmd = exec.Command("chromium", chromeOptions(url)...) case commandExists("chromium-browser"): - openCmd = exec.Command("chromium-browser", fmtChromeOptions(url)...) - case commandExists("firefox"): - openCmd = exec.Command("firefox", "--url="+url, "-safe-mode") + openCmd = exec.Command("chromium-browser", chromeOptions(url)...) case pathExists("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"): - openCmd = exec.Command("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", fmtChromeOptions(url)...) + openCmd = exec.Command("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", chromeOptions(url)...) default: - flog.Info("unable to find a browser to open: sshcode only supports firefox, chrome, and chromium") + err := browser.OpenURL(url) + if err != nil { + flog.Error("failed to open browser: %v", err) + } + return } + // We do not use CombinedOutput because if there is no chrome instance, this will block + // and become the parent process instead of using an existing chrome instance. err := openCmd.Start() if err != nil { - flog.Fatal("failed to open browser: %v", err) + flog.Error("failed to open browser: %v", err) } } -func fmtChromeOptions(url string) []string { +func chromeOptions(url string) []string { return []string{"--app=" + url, "--disable-extensions", "--disable-plugins"} } +func firefoxOptions(url string) []string { + return []string{"--url=" + url, "-safe-mode"} +} + // Checks if a command exists locally. func commandExists(name string) bool { _, err := exec.LookPath(name) From efca7a7c786614d1419f5e8caef4a436b86af76f Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 19 Apr 2019 21:22:43 -0400 Subject: [PATCH 2/2] Remove useless firefox code --- main.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/main.go b/main.go index b0f84f1..8ad18fa 100644 --- a/main.go +++ b/main.go @@ -166,10 +166,6 @@ func chromeOptions(url string) []string { return []string{"--app=" + url, "--disable-extensions", "--disable-plugins"} } -func firefoxOptions(url string) []string { - return []string{"--url=" + url, "-safe-mode"} -} - // Checks if a command exists locally. func commandExists(name string) bool { _, err := exec.LookPath(name)