aboutsummaryrefslogtreecommitdiffstats
path: root/lib/open.go
blob: 21bee748126823389deb1e37a1ddb89adc0dcb69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package lib

import (
	"fmt"
	"os/exec"
	"runtime"
	"strings"

	"git.sr.ht/~rjarry/go-opt"
	"github.com/danwakefield/fnmatch"

	"git.sr.ht/~rjarry/aerc/config"
	"git.sr.ht/~rjarry/aerc/log"
)

func XDGOpenMime(
	uri string, mimeType string, args string,
) error {
	if len(args) == 0 {
		// no explicit command provided, lookup opener from mime type
		for _, o := range config.Openers {
			if fnmatch.Match(o.Mime, mimeType, 0) {
				args = o.Args
				break
			}
		}
	}
	if len(args) == 0 {
		// no opener defined in config, fallback to default
		if runtime.GOOS == "darwin" {
			args = "open"
		} else {
			args = "xdg-open"
		}
	}

	// Escape URI special characters
	uri = opt.QuoteArg(uri)
	if strings.Contains(args, "{}") {
		// found {} placeholder in args, replace with uri
		args = strings.Replace(args, "{}", uri, 1)
	} else {
		// no {} placeholder in args, add uri at the end
		args = args + " " + uri
	}

	log.Tracef("running command: %v", args)
	cmd := exec.Command("sh", "-c", args)
	out, err := cmd.CombinedOutput()
	log.Debugf("command: %v exited. err=%v out=%s", args, err, out)
	if err != nil {
		return fmt.Errorf("%v: %w", args, err)
	}
	return nil
}