1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package browser provides utilities for interacting with users' browsers.
15 // Commands returns a list of possible commands to use to open a url.
16 func Commands() [][]string {
18 if exe := os.Getenv("BROWSER"); exe != "" {
19 cmds = append(cmds, []string{exe})
23 cmds = append(cmds, []string{"/usr/bin/open"})
25 cmds = append(cmds, []string{"cmd", "/c", "start"})
27 if os.Getenv("DISPLAY") != "" {
28 // xdg-open is only for use in a desktop environment.
29 cmds = append(cmds, []string{"xdg-open"})
34 []string{"google-chrome"},
41 // Open tries to open url in a browser and reports whether it succeeded.
42 func Open(url string) bool {
43 for _, args := range Commands() {
44 cmd := exec.Command(args[0], append(args[1:], url)...)
45 if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
52 // appearsSuccessful reports whether the command appears to have run successfully.
53 // If the command runs longer than the timeout, it's deemed successful.
54 // If the command runs within the timeout, it's deemed successful if it exited cleanly.
55 func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
56 errc := make(chan error, 1)
62 case <-time.After(timeout):