summaryrefslogtreecommitdiff
path: root/libgo/go/cmd/internal/browser/browser.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/cmd/internal/browser/browser.go')
-rw-r--r--libgo/go/cmd/internal/browser/browser.go25
1 files changed, 23 insertions, 2 deletions
diff --git a/libgo/go/cmd/internal/browser/browser.go b/libgo/go/cmd/internal/browser/browser.go
index 897086f4717..6867c85d232 100644
--- a/libgo/go/cmd/internal/browser/browser.go
+++ b/libgo/go/cmd/internal/browser/browser.go
@@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"runtime"
+ "time"
)
// Commands returns a list of possible commands to use to open a url.
@@ -23,7 +24,10 @@ func Commands() [][]string {
case "windows":
cmds = append(cmds, []string{"cmd", "/c", "start"})
default:
- cmds = append(cmds, []string{"xdg-open"})
+ if os.Getenv("DISPLAY") != "" {
+ // xdg-open is only for use in a desktop environment.
+ cmds = append(cmds, []string{"xdg-open"})
+ }
}
cmds = append(cmds,
[]string{"chrome"},
@@ -38,9 +42,26 @@ func Commands() [][]string {
func Open(url string) bool {
for _, args := range Commands() {
cmd := exec.Command(args[0], append(args[1:], url)...)
- if cmd.Start() == nil {
+ if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
return true
}
}
return false
}
+
+// appearsSuccessful reports whether the command appears to have run successfully.
+// If the command runs longer than the timeout, it's deemed successful.
+// If the command runs within the timeout, it's deemed successful if it exited cleanly.
+func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
+ errc := make(chan error, 1)
+ go func() {
+ errc <- cmd.Wait()
+ }()
+
+ select {
+ case <-time.After(timeout):
+ return true
+ case err := <-errc:
+ return err == nil
+ }
+}