-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.go
More file actions
38 lines (32 loc) · 1019 Bytes
/
browser.go
File metadata and controls
38 lines (32 loc) · 1019 Bytes
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
package main
import (
"context"
"fmt"
"os/exec"
"runtime"
)
// browserCommand returns the executable name and arguments for opening a URL
// on the given OS. This is extracted for testability.
func browserCommand(goos, url string) (name string, args []string) {
switch goos {
case "darwin":
return "open", []string{url}
case "windows":
return "rundll32", []string{"url.dll,FileProtocolHandler", url}
default:
return "xdg-open", []string{url}
}
}
// openBrowser attempts to open url in the user's default browser.
// Returns an error if launching the browser fails, but callers should
// always print the URL as a fallback regardless of the error.
func openBrowser(ctx context.Context, url string) error {
name, args := browserCommand(runtime.GOOS, url)
cmd := exec.CommandContext(ctx, name, args...)
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to open browser: %w", err)
}
// Detach — we don't wait for the browser to close.
go func() { _ = cmd.Wait() }()
return nil
}