realize/server/open.go

38 lines
636 B
Go
Raw Normal View History

2016-09-01 22:34:48 +00:00
package server
import (
"bytes"
"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
2016-09-01 22:34:48 +00:00
func Open(url string) (io.Writer, error) {
goos := runtime.GOOS
open, err := cmd[goos]
2016-12-17 16:20:16 +00:00
if !err {
return nil, fmt.Errorf("operating system %q is not supported", goos)
2016-09-01 22:34:48 +00:00
}
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
}