2019-07-03 00:21:34 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
2022-09-30 10:52:49 +02:00
|
|
|
"fmt"
|
2019-07-03 00:21:34 +02:00
|
|
|
"os/exec"
|
2021-01-30 11:33:31 +01:00
|
|
|
"runtime"
|
2022-09-30 14:12:07 +02:00
|
|
|
"strings"
|
2022-03-22 09:52:27 +01:00
|
|
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2019-07-03 00:21:34 +02:00
|
|
|
)
|
|
|
|
|
2022-09-30 10:52:49 +02:00
|
|
|
func XDGOpen(uri string) error {
|
2022-09-30 14:12:07 +02:00
|
|
|
return XDGOpenMime(uri, "", nil, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func XDGOpenMime(
|
|
|
|
uri string, mimeType string,
|
|
|
|
openers map[string][]string, args []string,
|
|
|
|
) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
// no explicit command provided, lookup opener from mime type
|
|
|
|
opener, ok := openers[mimeType]
|
|
|
|
if ok {
|
|
|
|
args = opener
|
|
|
|
} else {
|
|
|
|
// no opener defined in config, fallback to default
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
args = append(args, "open")
|
|
|
|
} else {
|
|
|
|
args = append(args, "xdg-open")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for ; i < len(args); i++ {
|
|
|
|
if strings.Contains(args[i], "{}") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i < len(args) {
|
|
|
|
// found {} placeholder in args, replace with uri
|
|
|
|
args[i] = strings.Replace(args[i], "{}", uri, 1)
|
|
|
|
} else {
|
|
|
|
// no {} placeholder in args, add uri at the end
|
|
|
|
args = append(args, uri)
|
2021-01-30 11:33:31 +01:00
|
|
|
}
|
2022-09-30 14:12:07 +02:00
|
|
|
|
2022-09-30 10:52:49 +02:00
|
|
|
logging.Infof("running command: %v", args)
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
logging.Debugf("command: %v exited. err=%v out=%s", args, err, out)
|
2021-01-30 11:33:31 +01:00
|
|
|
if err != nil {
|
2022-09-30 10:52:49 +02:00
|
|
|
return fmt.Errorf("%v: %w", args, err)
|
2021-01-30 11:33:31 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|