-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.go
More file actions
34 lines (28 loc) · 808 Bytes
/
browser.go
File metadata and controls
34 lines (28 loc) · 808 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
package main
import (
"context"
"fmt"
"os/exec"
"runtime"
)
// 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 {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.CommandContext(ctx, "open", url)
case "windows":
cmd = exec.CommandContext(ctx, "cmd", "/c", "start", url)
default:
// Linux and other Unix-like systems
cmd = exec.CommandContext(ctx, "xdg-open", url)
}
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
}