-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbrowser.go
More file actions
111 lines (100 loc) · 2.76 KB
/
browser.go
File metadata and controls
111 lines (100 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
func openBrowser(url string) {
time.Sleep(200 * time.Millisecond)
if tryOpenBrowser(browserCommandSpecs(runtime.GOOS, url, systemIsWSL(), commandExists), runBrowserCommand) {
return
}
fmt.Fprintf(os.Stderr, "Warning: could not open browser automatically; open %s manually\n", url)
}
type browserCommandSpec struct {
name string
args []string
}
func tryOpenBrowser(specs []browserCommandSpec, run func(browserCommandSpec) error) bool {
for _, spec := range specs {
if err := run(spec); err == nil {
return true
}
}
return false
}
func runBrowserCommand(spec browserCommandSpec) error {
return exec.Command(spec.name, spec.args...).Run()
}
func browserCommandSpecs(goos, url string, isWSL bool, hasCommand func(string) bool) []browserCommandSpec {
switch goos {
case "darwin":
return []browserCommandSpec{{name: "open", args: []string{url}}}
case "windows":
// rundll32 url.dll is the most reliable way to open the default
// browser on every supported Windows version. cmd /c start works too
// but requires careful quoting around URLs containing & or %.
return []browserCommandSpec{
{name: "rundll32", args: []string{"url.dll,FileProtocolHandler", url}},
}
case "linux":
var specs []browserCommandSpec
if isWSL {
if hasCommand("wslview") {
specs = append(specs, browserCommandSpec{name: "wslview", args: []string{url}})
}
if hasCommand("powershell.exe") {
specs = append(specs, browserCommandSpec{
name: "powershell.exe",
args: []string{
"-NoProfile",
"-NonInteractive",
"-Command",
"Start-Process " + powershellSingleQuote(url),
},
})
}
if hasCommand("cmd.exe") {
specs = append(specs, browserCommandSpec{
name: "cmd.exe",
args: []string{"/c", `start "" ` + cmdDoubleQuote(url)},
})
}
}
if hasCommand("xdg-open") {
specs = append(specs, browserCommandSpec{name: "xdg-open", args: []string{url}})
}
return specs
default:
return nil
}
}
func powershellSingleQuote(s string) string {
return "'" + strings.ReplaceAll(s, "'", "''") + "'"
}
func cmdDoubleQuote(s string) string {
return `"` + strings.ReplaceAll(s, `"`, `""`) + `"`
}
func systemIsWSL() bool {
versionData, err := os.ReadFile("/proc/version")
if err != nil {
versionData = nil
}
return looksLikeWSL(runtime.GOOS, os.Getenv("WSL_DISTRO_NAME"), os.Getenv("WSL_INTEROP"), string(versionData))
}
func looksLikeWSL(goos, distroName, interop, procVersion string) bool {
if goos != "linux" {
return false
}
if distroName != "" || interop != "" {
return true
}
return strings.Contains(strings.ToLower(procVersion), "microsoft")
}
func commandExists(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}