2020-11-10 14:22:45 +00:00
|
|
|
//+build !windows
|
|
|
|
|
2018-04-06 02:15:21 +00:00
|
|
|
package open
|
|
|
|
|
|
|
|
import (
|
2018-04-19 06:32:57 +00:00
|
|
|
"fmt"
|
2018-04-06 02:15:21 +00:00
|
|
|
"os/exec"
|
2018-04-18 03:53:52 +00:00
|
|
|
"strings"
|
2018-04-06 02:15:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var openCmd string
|
|
|
|
var possibleCmds = []string{
|
2018-04-19 06:33:53 +00:00
|
|
|
"xdg-open", // generic linux
|
2018-04-19 06:22:17 +00:00
|
|
|
"gvfs-open", // gnome linux
|
|
|
|
"gnome-open", // gnome linux
|
|
|
|
"kde-open", // kde linux
|
2020-12-04 11:26:15 +00:00
|
|
|
"kde-open5", // kde linux
|
2018-04-19 06:22:17 +00:00
|
|
|
"exo-open", // xfce linux
|
|
|
|
"enlightenment_open", // enlightenment linux
|
|
|
|
"open", // mac
|
|
|
|
"start", // windows
|
|
|
|
"cygstart", // windows
|
2018-04-06 02:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-04-19 06:28:19 +00:00
|
|
|
for _, cmd := range possibleCmds {
|
2018-04-19 06:32:57 +00:00
|
|
|
out, err := exec.Command("/bin/bash", "-c", fmt.Sprintf("%s %s %s", "command", "-v", cmd)).Output()
|
2018-04-06 02:15:21 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-18 03:53:52 +00:00
|
|
|
bin := strings.TrimSpace(string(out))
|
2018-04-06 02:15:21 +00:00
|
|
|
if bin != "" {
|
2018-04-19 06:28:19 +00:00
|
|
|
openCmd = bin
|
2018-04-19 06:26:38 +00:00
|
|
|
break
|
2018-04-06 02:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL open url
|
2020-11-15 22:59:53 +00:00
|
|
|
func URL(url string) error {
|
2018-04-06 02:15:21 +00:00
|
|
|
if openCmd != "" {
|
2020-11-15 22:59:53 +00:00
|
|
|
return exec.Command(openCmd, url).Run()
|
2018-04-06 02:15:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-28 05:03:10 +00:00
|
|
|
|
|
|
|
// CommandExists returns true if an 'open' command exists
|
|
|
|
func CommandExists() bool {
|
|
|
|
return openCmd != ""
|
|
|
|
}
|