openURLPlatformSpecific is a helper function that opens a URL using OS-specific commands. This serves as a fallback mechanism for OpenURL. Parameters: - url: The URL to open. Returns: - An error if the URL cannot be opened, otherwise nil.
(url string)
| 45 | // Returns: |
| 46 | // - An error if the URL cannot be opened, otherwise nil. |
| 47 | func openURLPlatformSpecific(url string) error { |
| 48 | var cmd *exec.Cmd |
| 49 | |
| 50 | switch runtime.GOOS { |
| 51 | case "darwin": // macOS |
| 52 | cmd = exec.Command("open", url) |
| 53 | case "windows": |
| 54 | cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url) |
| 55 | case "linux": |
| 56 | // Try common Linux browsers in order of preference |
| 57 | browsers := []string{"xdg-open", "x-www-browser", "www-browser", "firefox", "chromium", "google-chrome"} |
| 58 | for _, browser := range browsers { |
| 59 | if _, err := exec.LookPath(browser); err == nil { |
| 60 | cmd = exec.Command(browser, url) |
| 61 | break |
| 62 | } |
| 63 | } |
| 64 | if cmd == nil { |
| 65 | return fmt.Errorf("no suitable browser found on Linux system") |
| 66 | } |
| 67 | default: |
| 68 | return fmt.Errorf("unsupported operating system: %s", runtime.GOOS) |
| 69 | } |
| 70 | |
| 71 | log.Debugf("Running command: %s %v", cmd.Path, cmd.Args[1:]) |
| 72 | err := cmd.Start() |
| 73 | if err != nil { |
| 74 | return fmt.Errorf("failed to start browser command: %w", err) |
| 75 | } |
| 76 | |
| 77 | log.Debug("Successfully opened URL using platform-specific command") |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // IsAvailable checks if the system has a command available to open a web browser. |
| 82 | // It verifies the presence of necessary commands for the current operating system. |