2021-12-01 19:18:20 +03:00
|
|
|
//go:build linux || freebsd || netbsd || openbsd
|
2020-12-14 22:28:07 +03:00
|
|
|
// +build linux freebsd netbsd openbsd
|
|
|
|
|
|
|
|
//nolint:goerr113
|
|
|
|
package sysopen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open opens `path` in default system viewer. It tries to do so using
|
|
|
|
// xdg-open. It only works if there is a display server working.
|
|
|
|
func Open(path string) (string, error) {
|
|
|
|
var (
|
|
|
|
xorgDisplay = os.Getenv("DISPLAY")
|
|
|
|
waylandDisplay = os.Getenv("WAYLAND_DISPLAY")
|
|
|
|
xdgOpenPath, xdgOpenNotFoundErr = exec.LookPath("xdg-open")
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case xorgDisplay == "" && waylandDisplay == "":
|
|
|
|
return "", fmt.Errorf("no display server was found. " +
|
2021-12-07 23:28:34 +03:00
|
|
|
"You may set a default command in the config")
|
2020-12-14 22:28:07 +03:00
|
|
|
case xdgOpenNotFoundErr == nil:
|
|
|
|
// Use start rather than run or output in order
|
|
|
|
// to make application run in background.
|
2021-12-23 01:49:07 +03:00
|
|
|
proc := exec.Command(xdgOpenPath, path)
|
|
|
|
if err := proc.Start(); err != nil {
|
2020-12-14 22:28:07 +03:00
|
|
|
return "", err
|
|
|
|
}
|
2021-12-23 04:39:17 +03:00
|
|
|
//nolint:errcheck
|
2021-12-23 01:49:07 +03:00
|
|
|
go proc.Wait() // Prevent zombies, see #219
|
2020-12-14 22:28:07 +03:00
|
|
|
return "Opened in default system viewer", nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("could not determine default system viewer. " +
|
2021-12-07 23:28:34 +03:00
|
|
|
"Set a catch-all command in the config")
|
2020-12-14 22:28:07 +03:00
|
|
|
}
|
|
|
|
}
|