Improve syscall.Exec (macOS).

This commit is contained in:
Nuno Cruces 2021-03-03 12:15:29 +00:00
parent 12a6bd248c
commit 79197f2d9e

View file

@ -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)
}
}