aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-09-30 10:52:49 +0200
committerRobin Jarry <robin@jarry.cc>2022-10-01 15:46:49 +0200
commit92ba132d70fe1d9afabe3cf4f23376025ccff897 (patch)
tree9be550d0c25c9d0367a144317312774d3e3c53e4 /lib
parent8e53d330614fb5c526372e6bdf508341bb6f154c (diff)
downloadaerc-92ba132d70fe1d9afabe3cf4f23376025ccff897.tar.gz
open: simplify code
There is no need for convoluted channels and other async fanciness. Expose a single XDGOpen static function that runs a command and returns an error if any. Caller is responsible of running this in an async goroutine if needed. Signed-off-by: Robin Jarry <robin@jarry.cc> Tested-by: Tim Culverhouse <tim@timculverhouse.com> Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'lib')
-rw-r--r--lib/open.go58
1 files changed, 9 insertions, 49 deletions
diff --git a/lib/open.go b/lib/open.go
index c29ed009..a189980a 100644
--- a/lib/open.go
+++ b/lib/open.go
@@ -1,65 +1,25 @@
package lib
import (
+ "fmt"
"os/exec"
"runtime"
"git.sr.ht/~rjarry/aerc/logging"
)
-var openBin string = "xdg-open"
-
-func init() {
+func XDGOpen(uri string) error {
+ openBin := "xdg-open"
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},
- }
-}
-
-// 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]
- xdg.args = append(args, filename) //nolint:gocritic // intentional append to different slice
-}
-
-// 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()
+ args := []string{openBin, uri}
+ 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)
if err != nil {
- xdg.errCh <- err // for callers that just check the error from Wait()
- close(xdg.errCh)
- return err
+ return fmt.Errorf("%v: %w", args, err)
}
- go func() {
- defer logging.PanicHandler()
-
- xdg.errCh <- xdg.cmd.Wait()
- close(xdg.errCh)
- }()
return nil
}
-
-// Wait for the xdg-open command to complete
-// The xdgOpen must have been started
-func (xdg *xdgOpen) Wait() error {
- return <-xdg.errCh
-}