posh/pkg/util/browser/open.go
Kevin Franklin Kim a0e9b8bf64
feat: go 1.24.0
2025-02-19 11:46:16 +01:00

35 lines
709 B
Go

package browser
import (
"context"
"net/url"
"os/exec"
"runtime"
"github.com/pkg/errors"
)
func OpenRawURL(ctx context.Context, u string) error {
if u, err := url.Parse(u); err != nil {
return err
} else {
return OpenURL(ctx, u)
}
}
func OpenURL(ctx context.Context, u *url.URL) error {
if u == nil {
return errors.New("empty url")
}
switch runtime.GOOS {
case "linux":
return exec.CommandContext(ctx, "xdg-open", u.String()).Start()
case "windows":
return exec.CommandContext(ctx, "rundll32", "url.dll,FileProtocolHandler", u.String()).Start()
case "darwin":
return exec.CommandContext(ctx, "open", u.String()).Start()
default:
return errors.New("unsupported platform")
}
}