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 ( import (
"context" "context"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -19,10 +20,16 @@ func Run(ctx context.Context, script string, data interface{}) ([]byte, error) {
script = buf.String() script = buf.String()
if Command { if Command {
path, err := exec.LookPath("osascript") // Try to use syscall.Exec, fallback to exec.Command.
if err == nil { if path, err := exec.LookPath("osascript"); err != nil {
os.Stderr.Close() } else if t, err := ioutil.TempFile("", ""); err != nil {
syscall.Exec(path, []string{"osascript", "-l", "JavaScript", "-e", script}, 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)
} }
} }