@@ -2,60 +2,22 @@ package utils
22
33import (
44 "bytes"
5- "errors"
65 "fmt"
7- "os"
8- "os/exec"
9- "runtime"
106 "time"
117
12- "github.com/kballard/go-shellquote "
8+ "github.com/cli/cli/pkg/browser "
139 md "github.com/vilmibm/go-termd"
1410)
1511
12+ // OpenInBrowser opens the url in a web browser based on OS and $BROWSER environment variable
1613func OpenInBrowser (url string ) error {
17- browser := os .Getenv ("BROWSER" )
18- if browser == "" {
19- browser = searchBrowserLauncher (runtime .GOOS )
20- } else {
21- browser = os .ExpandEnv (browser )
22- }
23-
24- if browser == "" {
25- return errors .New ("Please set $BROWSER to a web launcher" )
26- }
27-
28- browserArgs , err := shellquote .Split (browser )
14+ browseCmd , err := browser .Command (url )
2915 if err != nil {
3016 return err
3117 }
32-
33- endingArgs := append (browserArgs [1 :], url )
34- browseCmd := exec .Command (browserArgs [0 ], endingArgs ... )
3518 return PrepareCmd (browseCmd ).Run ()
3619}
3720
38- func searchBrowserLauncher (goos string ) (browser string ) {
39- switch goos {
40- case "darwin" :
41- browser = "open"
42- case "windows" :
43- browser = "cmd /c start"
44- default :
45- candidates := []string {"xdg-open" , "cygstart" , "x-www-browser" , "firefox" ,
46- "opera" , "mozilla" , "netscape" }
47- for _ , b := range candidates {
48- path , err := exec .LookPath (b )
49- if err == nil {
50- browser = path
51- break
52- }
53- }
54- }
55-
56- return browser
57- }
58-
5921func normalizeNewlines (d []byte ) []byte {
6022 d = bytes .Replace (d , []byte ("\r \n " ), []byte ("\n " ), - 1 )
6123 d = bytes .Replace (d , []byte ("\r " ), []byte ("\n " ), - 1 )
0 commit comments