From 79197f2d9e02e3f9ae7f03726a3e7b074a0c8be9 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 3 Mar 2021 12:15:29 +0000 Subject: [PATCH] Improve syscall.Exec (macOS). --- internal/zenutil/run_darwin.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index 4fbdd0d..bb2fd88 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -2,6 +2,7 @@ package zenutil import ( "context" + "io/ioutil" "os" "os/exec" "strings" @@ -19,10 +20,16 @@ func Run(ctx context.Context, script string, data interface{}) ([]byte, error) { script = buf.String() if Command { - path, err := exec.LookPath("osascript") - if err == nil { - os.Stderr.Close() - syscall.Exec(path, []string{"osascript", "-l", "JavaScript", "-e", script}, nil) + // Try to use syscall.Exec, fallback to exec.Command. + if path, err := exec.LookPath("osascript"); err != nil { + } else if t, err := ioutil.TempFile("", ""); err != nil { + } else if _, err := t.WriteString(script); err != nil { + } else if _, err := t.Seek(0, 0); err != nil { + } else if err := os.Remove(t.Name()); err != nil { + } else if err := syscall.Dup2(int(t.Fd()), syscall.Stdin); err != nil { + } else if err := os.Stderr.Close(); err != nil { + } else { + syscall.Exec(path, []string{"osascript", "-l", "JavaScript"}, nil) } }