blob: 50326333b63d4e0f0251bb1941264a9723875050 [file] [log] [blame]
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +00001// Package browser provides functions for opening URLs in a web browser.
2package browser
3
4import (
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +00005 "fmt"
6 "os"
7 "os/exec"
8 "runtime"
9)
10
11// Open opens the specified URL in the system's default browser.
12// It detects the OS and uses the appropriate command:
13// - 'open' for macOS
14// - 'cmd /c start' for Windows
15// - 'xdg-open' for Linux and other Unix-like systems
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -070016func Open(url string) {
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000017 var cmd *exec.Cmd
18 switch runtime.GOOS {
19 case "darwin":
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -070020 cmd = exec.Command("open", url)
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000021 case "windows":
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -070022 cmd = exec.Command("cmd", "/c", "start", url)
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000023 default: // Linux and other Unix-like systems
Josh Bleecher Snydere54b00a2025-04-30 16:48:02 -070024 cmd = exec.Command("xdg-open", url)
Josh Bleecher Snyder78707d62025-04-30 21:06:49 +000025 }
26 if b, err := cmd.CombinedOutput(); err != nil {
27 fmt.Fprintf(os.Stderr, "failed to open browser: %v: %s\n", err, b)
28 }
29}