realize/server/open.go

37 lines
623 B
Go
Raw Normal View History

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-12-17 16:20:16 +00:00
open, err := cmd[runtime.GOOS];
if !err {
2016-09-01 22:34:48 +00:00
return nil, errors.New("This operating system is not supported.")
}
2016-12-17 16:20:16 +00:00
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
}