diff options
author | Ian Lance Taylor <iant@golang.org> | 2017-09-14 17:11:35 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2017-09-14 17:11:35 +0000 |
commit | bc998d034f45d1828a8663b2eed928faf22a7d01 (patch) | |
tree | 8d262a22ca7318f4bcd64269fe8fe9e45bcf8d0f /libgo/go/cmd/internal/browser/browser.go | |
parent | a41a6142df74219f596e612d3a7775f68ca6e96f (diff) |
libgo: update to go1.9
Reviewed-on: https://go-review.googlesource.com/63753
From-SVN: r252767
Diffstat (limited to 'libgo/go/cmd/internal/browser/browser.go')
-rw-r--r-- | libgo/go/cmd/internal/browser/browser.go | 25 |
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 + } +} |