Commits

Miki Tebeka committed 9be5557

Initial
;

  • Participants

Comments (0)

Files changed (3)

+export GOPATH := $(shell dirname $(shell dirname $(PWD)))
+PACKAGE := desktop
+
+all:
+	go build $(PACKAGE)
+
+test:
+	go test -v $(PACKAGE)
+
+fix:
+	go fix $(PACKAGE)
+
+doc:
+	go doc $(PACKAGE)
+
+install:
+	go install $(PACKAGE)
+
+README.html: README.rst
+	rst2html $< > $@
+
+.PHONY: all test install fix doc
+/* The desktop package exposes Open function which acts like the user clicked on a uri. */
+package desktop
+
+import (
+	"fmt"
+	"os/exec"
+	"runtime"
+)
+
+var commands = map[string]string{
+	"windows": "start",
+	"darwin":  "open",
+	"linux":   "xdg-open",
+}
+
+// Open calls the OS default program for uri
+func Open(uri string) error {
+	run, ok := commands[runtime.GOOS]
+	if !ok {
+		return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
+	}
+
+	cmd := exec.Command(run, uri)
+	return cmd.Start()
+}

File desktop_test.go

+package desktop
+
+import (
+	"runtime"
+	"testing"
+)
+
+func TestOpenBadPlatform(t *testing.T) {
+	saved := commands[runtime.GOOS]
+	delete(commands, runtime.GOOS)
+	err := Open("")
+	commands[runtime.GOOS] = saved
+
+	if err == nil {
+		t.Fatalf("open succeeded on unkown platform")
+	}
+}