2019-07-03 00:21:34 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2021-01-30 11:33:31 +01:00
|
|
|
"runtime"
|
2022-03-22 09:52:27 +01:00
|
|
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2019-07-03 00:21:34 +02:00
|
|
|
)
|
|
|
|
|
2021-01-30 11:33:31 +01:00
|
|
|
var openBin string = "xdg-open"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
openBin = "open"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type xdgOpen struct {
|
|
|
|
args []string
|
|
|
|
errCh chan (error)
|
|
|
|
cmd *exec.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewXDGOpen returns a handler for opening a file via the system handler xdg-open
|
|
|
|
// or comparable tools on other OSs than Linux
|
|
|
|
func NewXDGOpen(filename string) *xdgOpen {
|
|
|
|
errch := make(chan error, 1)
|
|
|
|
return &xdgOpen{
|
|
|
|
errCh: errch,
|
|
|
|
args: []string{filename},
|
2020-07-06 21:14:15 +02:00
|
|
|
}
|
2021-01-30 11:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetArgs sets additional arguments to the open command prior to the filename
|
|
|
|
func (xdg *xdgOpen) SetArgs(args []string) {
|
|
|
|
args = append([]string{}, args...) // don't overwrite array of caller
|
|
|
|
filename := xdg.args[len(xdg.args)-1]
|
2022-07-31 14:32:48 +02:00
|
|
|
xdg.args = append(args, filename) //nolint:gocritic // intentional append to different slice
|
2021-01-30 11:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the open handler.
|
|
|
|
// Returns an error if the command could not be started.
|
|
|
|
// Use Wait to wait for the commands completion and to check the error.
|
|
|
|
func (xdg *xdgOpen) Start() error {
|
|
|
|
xdg.cmd = exec.Command(openBin, xdg.args...)
|
|
|
|
err := xdg.cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
xdg.errCh <- err // for callers that just check the error from Wait()
|
|
|
|
close(xdg.errCh)
|
|
|
|
return err
|
|
|
|
}
|
2020-07-06 21:14:15 +02:00
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2021-01-30 11:33:31 +01:00
|
|
|
xdg.errCh <- xdg.cmd.Wait()
|
|
|
|
close(xdg.errCh)
|
2020-07-06 21:14:15 +02:00
|
|
|
}()
|
2021-01-30 11:33:31 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the xdg-open command to complete
|
|
|
|
// The xdgOpen must have been started
|
|
|
|
func (xdg *xdgOpen) Wait() error {
|
|
|
|
return <-xdg.errCh
|
2019-07-03 00:21:34 +02:00
|
|
|
}
|