github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/src/cmd/internal/browser/browser.go (about)

     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.
     4  
     5  // Package browser provides utilities for interacting with users' browsers.
     6  package browser
     7  
     8  import (
     9  	"os"
    10  	"os/exec"
    11  	"runtime"
    12  )
    13  
    14  // Commands returns a list of possible commands to use to open a url.
    15  func Commands() [][]string {
    16  	var cmds [][]string
    17  	if exe := os.Getenv("BROWSER"); exe != "" {
    18  		cmds = append(cmds, []string{exe})
    19  	}
    20  	switch runtime.GOOS {
    21  	case "darwin":
    22  		cmds = append(cmds, []string{"/usr/bin/open"})
    23  	case "windows":
    24  		cmds = append(cmds, []string{"cmd", "/c", "start"})
    25  	default:
    26  		cmds = append(cmds, []string{"xdg-open"})
    27  	}
    28  	cmds = append(cmds, []string{"chrome"}, []string{"google-chrome"}, []string{"firefox"})
    29  	return cmds
    30  }
    31  
    32  // Open tries to open url in a browser and reports whether it succeeded.
    33  func Open(url string) bool {
    34  	for _, args := range Commands() {
    35  		cmd := exec.Command(args[0], append(args[1:], url)...)
    36  		if cmd.Start() == nil {
    37  			return true
    38  		}
    39  	}
    40  	return false
    41  }