From 8c269b0c2f7b99ca33d9f9f961e52f5f6c5f954e Mon Sep 17 00:00:00 2001 From: alessio Date: Fri, 2 Sep 2016 00:34:48 +0200 Subject: [PATCH] open server url --- server/open.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 server/open.go diff --git a/server/open.go b/server/open.go new file mode 100644 index 0000000..5666b5a --- /dev/null +++ b/server/open.go @@ -0,0 +1,33 @@ +package server + +import ( + "bytes" + "errors" + "io" + "os/exec" + "runtime" +) + +var cli map[string]string +var stderr bytes.Buffer + +func init() { + cli = map[string]string{ + "windows": "start", + "darwin": "open", + "linux": "xdg-open", + } +} + +func Open(url string) (io.Writer, error) { + if open, err := cli[runtime.GOOS]; !err { + 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 +}