54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
// Package browser opens a URL in the operator's default browser. It's
|
|
// a thin OS-aware wrapper around xdg-open / open / rundll32 so both
|
|
// the sherlock CLI and the MCP binaries can share the same behaviour
|
|
// without depending on each other.
|
|
package browser
|
|
|
|
import (
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// Open starts the OS's default browser pointed at url, then returns
|
|
// immediately. The browser process is reaped in a background goroutine.
|
|
// Errors come from starting the helper, not from anything the browser
|
|
// itself reports — there's no portable way to know whether the user
|
|
// actually loaded the page.
|
|
func Open(url string) error {
|
|
return OpenWith("", url)
|
|
}
|
|
|
|
// OpenWith starts browser pointed at url, then returns immediately. If
|
|
// browser is empty, it falls back to the OS default helper used by Open.
|
|
func OpenWith(browser, url string) error {
|
|
bin, args := commandFor(browser, url, runtime.GOOS)
|
|
cmd := exec.Command(bin, args...)
|
|
if err := cmd.Start(); err != nil {
|
|
return err
|
|
}
|
|
go func() { _ = cmd.Wait() }()
|
|
return nil
|
|
}
|
|
|
|
func commandFor(browser, url, goos string) (string, []string) {
|
|
if browser = strings.TrimSpace(browser); browser != "" {
|
|
return browser, []string{url}
|
|
}
|
|
|
|
var bin string
|
|
var args []string
|
|
switch goos {
|
|
case "darwin":
|
|
bin = "open"
|
|
args = []string{url}
|
|
case "windows":
|
|
bin = "rundll32"
|
|
args = []string{"url.dll,FileProtocolHandler", url}
|
|
default:
|
|
bin = "xdg-open"
|
|
args = []string{url}
|
|
}
|
|
return bin, args
|
|
}
|