SHARE
TWEET

Untitled

a guest Aug 19th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // OpenBrowser opens up the provided URL in a browser
  2. func OpenBrowser(url string) error {
  3.     var cmd *exec.Cmd
  4.     switch runtime.GOOS {
  5.     case "openbsd":
  6.         fallthrough
  7.     case "linux":
  8.         cmd = exec.Command("xdg-open", url)
  9.     case "darwin":
  10.         cmd = exec.Command("open", url)
  11.     case "windows":
  12.         r := strings.NewReplacer("&", "^&")
  13.         cmd = exec.Command("cmd", "/c", "start", r.Replace(url))
  14.     }
  15.     if cmd != nil {
  16.         cmd.Stdout = os.Stdout
  17.         cmd.Stderr = os.Stderr
  18.         err := cmd.Start()
  19.         if err != nil {
  20.             log.Printf("Failed to open browser due to error %v", err)
  21.             return fmt.Errorf("Failed to open browser: " + err.Error())
  22.         }
  23.         err = cmd.Wait()
  24.         if err != nil {
  25.             log.Printf("Failed to wait for open browser command to finish due to error %v", err)
  26.             return fmt.Errorf("Failed to wait for open browser command to finish: " + err.Error())
  27.         }
  28.         return nil
  29.     } else {
  30.         return errors.New("unsupported platform")
  31.     }
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top