2016-09-01 22:34:48 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-13 12:50:35 +00:00
|
|
|
"fmt"
|
2016-09-01 22:34:48 +00:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
2016-11-01 09:56:12 +00:00
|
|
|
var cmd map[string]string
|
2016-09-01 22:34:48 +00:00
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Init an associative array with the os supported
|
2016-09-01 22:34:48 +00:00
|
|
|
func init() {
|
2016-11-01 09:56:12 +00:00
|
|
|
cmd = map[string]string{
|
2016-09-01 22:34:48 +00:00
|
|
|
"windows": "start",
|
|
|
|
"darwin": "open",
|
|
|
|
"linux": "xdg-open",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Open a url in the default browser
|
2017-08-14 20:17:11 +00:00
|
|
|
func (s *Server) OpenURL(url string) (io.Writer, error) {
|
|
|
|
if s.Open {
|
|
|
|
open, err := cmd[runtime.GOOS]
|
|
|
|
if !err {
|
|
|
|
return nil, fmt.Errorf("operating system %q is not supported", runtime.GOOS)
|
|
|
|
}
|
|
|
|
cmd := exec.Command(open, url)
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return cmd.Stderr, err
|
|
|
|
}
|
2016-09-01 22:34:48 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|