(ctx context.Context, urlToOpen string)
| 11 | ) |
| 12 | |
| 13 | func Open(ctx context.Context, urlToOpen string) error { |
| 14 | if err := validate(urlToOpen); err != nil { |
| 15 | return err |
| 16 | } |
| 17 | |
| 18 | var cmd string |
| 19 | var args []string |
| 20 | |
| 21 | switch runtime.GOOS { |
| 22 | case "windows": |
| 23 | cmd = "rundll32" |
| 24 | args = []string{"url.dll,FileProtocolHandler", urlToOpen} |
| 25 | case "darwin": |
| 26 | cmd = "open" |
| 27 | args = []string{urlToOpen} |
| 28 | case "linux": |
| 29 | cmd = "xdg-open" |
| 30 | args = []string{urlToOpen} |
| 31 | default: |
| 32 | return fmt.Errorf("unsupported platform: %s", runtime.GOOS) |
| 33 | } |
| 34 | |
| 35 | err := exec.CommandContext(ctx, cmd, args...).Start() |
| 36 | if err != nil { |
| 37 | return fmt.Errorf("failed to open browser: %w", err) |
| 38 | } |
| 39 | |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | // validate rejects inputs that are unsafe to hand to the platform "open" |
| 44 | // helper (open, xdg-open, rundll32). Those helpers receive the URL as a |
no test coverage detected