Home | History | Annotate | Download | only in browser
      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,
     29 		[]string{"chrome"},
     30 		[]string{"google-chrome"},
     31 		[]string{"chromium"},
     32 		[]string{"firefox"},
     33 	)
     34 	return cmds
     35 }
     36 
     37 // Open tries to open url in a browser and reports whether it succeeded.
     38 func Open(url string) bool {
     39 	for _, args := range Commands() {
     40 		cmd := exec.Command(args[0], append(args[1:], url)...)
     41 		if cmd.Start() == nil {
     42 			return true
     43 		}
     44 	}
     45 	return false
     46 }
     47