web.go 739 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package util
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "runtime"
  6. )
  7. func OpenBrowser(url string) error {
  8. var err error
  9. switch runtime.GOOS {
  10. case "linux":
  11. err = exec.Command("xdg-open", url).Start()
  12. case "windows":
  13. err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
  14. case "darwin":
  15. err = exec.Command("open", url).Start()
  16. default:
  17. err = fmt.Errorf("unsupported platform")
  18. }
  19. return err
  20. }
  21. // checkAvailablePort 端口可用检测,netstat 只适合linux
  22. func checkAvailablePort(port int) (res bool) {
  23. checkStatement := fmt.Sprintf(`lsof -i:%d -t`, port)
  24. output, err := exec.Command("sh", "-c", checkStatement).Output()
  25. if err != nil {
  26. return false
  27. }
  28. if len(output) > 0 {
  29. return false
  30. }
  31. return true
  32. }