github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/cmd/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  // Taken from https://tip.golang.org/src/cmd/internal/browser/browser.go?m=text
     6  
     7  // Package browser provides utilities for interacting with users' browsers.
     8  package browser
     9  
    10  import (
    11  	"os"
    12  	"os/exec"
    13  	"runtime"
    14  )
    15  
    16  // Commands returns a list of possible commands to use to open a url.
    17  func Commands() [][]string {
    18  	var cmds [][]string
    19  	if exe := os.Getenv("BROWSER"); exe != "" {
    20  		cmds = append(cmds, []string{exe})
    21  	}
    22  	switch runtime.GOOS {
    23  	case "darwin":
    24  		cmds = append(cmds, []string{"/usr/bin/open"})
    25  	case "windows":
    26  		cmds = append(cmds, []string{"cmd", "/c", "start"})
    27  	default:
    28  		cmds = append(cmds, []string{"xdg-open"})
    29  	}
    30  	cmds = append(cmds,
    31  		[]string{"chrome"},
    32  		[]string{"google-chrome"},
    33  		[]string{"chromium"},
    34  		[]string{"firefox"},
    35  	)
    36  	return cmds
    37  }
    38  
    39  // Open tries to open url in a browser and reports whether it succeeded.
    40  func Open(url string) bool {
    41  	for _, args := range Commands() {
    42  		cmd := exec.Command(args[0], append(args[1:], url)...)
    43  		if cmd.Start() == nil {
    44  			return true
    45  		}
    46  	}
    47  	return false
    48  }