2016-09-01 22:34:48 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"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
|
2016-09-01 22:34:48 +00:00
|
|
|
func Open(url string) (io.Writer, error) {
|
2016-11-01 09:56:12 +00:00
|
|
|
if open, err := cmd[runtime.GOOS]; !err {
|
2016-09-01 22:34:48 +00:00
|
|
|
return nil, errors.New("This operating system is not supported.")
|
|
|
|
} else {
|
|
|
|
cmd := exec.Command(open, url)
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return cmd.Stderr, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|