打开浏览器(跨平台,支持windows、macos、linux)
(url string)
| 28 | |
| 29 | // 打开浏览器(跨平台,支持windows、macos、linux) |
| 30 | func OpenBrowser(url string) (err error) { |
| 31 | switch runtime.GOOS { |
| 32 | case "linux": |
| 33 | output, err := exec.Command("ls -l /usr/share/xsessions/").CombinedOutput() |
| 34 | if err == nil { |
| 35 | SmartIDELog.Debug(string(output)) |
| 36 | if !strings.Contains(string(output), "No such file or directory") { // 有图形化的界面,才打开 |
| 37 | err = exec.Command("xdg-open", url).Start() |
| 38 | } else { |
| 39 | return errors.New("当前非桌面版!") |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | case "windows": |
| 44 | err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() |
| 45 | case "darwin": |
| 46 | err = exec.Command("open", url).Start() |
| 47 | default: |
| 48 | err = fmt.Errorf("unsupported platform") |
| 49 | } |
| 50 | |
| 51 | return err |
| 52 | |
| 53 | } |
nothing calls this directly
no test coverage detected