2022-04-01 20:28:51 +00:00
|
|
|
package execuser
|
|
|
|
|
|
|
|
|
|
import (
|
2024-11-20 16:44:40 +00:00
|
|
|
"errors"
|
2022-04-01 20:28:51 +00:00
|
|
|
"fmt"
|
2024-10-31 19:24:42 +00:00
|
|
|
"io"
|
2022-04-01 20:28:51 +00:00
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// run uses macOS open command to start application as the current login user.
|
2024-11-22 19:30:13 +00:00
|
|
|
// Note that the child process spawns a new process in user space and thus it is not
|
|
|
|
|
// effective to add a context to this function to cancel the child process.
|
2024-10-31 19:24:42 +00:00
|
|
|
func run(path string, opts eopts) (lastLogs string, err error) {
|
2022-04-01 20:28:51 +00:00
|
|
|
info, err := os.Stat(path)
|
|
|
|
|
if err != nil {
|
2024-10-31 19:24:42 +00:00
|
|
|
return "", fmt.Errorf("stat path %q: %w", path, err)
|
2022-04-01 20:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
2024-10-31 19:24:42 +00:00
|
|
|
return "", fmt.Errorf("path is not an .app directory: %s", path)
|
2022-04-01 20:28:51 +00:00
|
|
|
}
|
|
|
|
|
var arg []string
|
|
|
|
|
if opts.stderrPath != "" {
|
|
|
|
|
arg = append(arg, "--stderr", opts.stderrPath)
|
|
|
|
|
}
|
2023-02-23 17:48:40 +00:00
|
|
|
|
|
|
|
|
// set environment variables
|
2022-04-01 20:28:51 +00:00
|
|
|
for _, nv := range opts.env {
|
|
|
|
|
arg = append(arg, "--env", fmt.Sprintf("%s=%s", nv[0], nv[1]))
|
|
|
|
|
}
|
2023-02-23 17:48:40 +00:00
|
|
|
|
|
|
|
|
// set the path to be executed
|
2022-04-01 20:28:51 +00:00
|
|
|
arg = append(arg, path)
|
2023-02-23 17:48:40 +00:00
|
|
|
|
|
|
|
|
// set the program arguments
|
|
|
|
|
if len(opts.args) > 0 {
|
|
|
|
|
arg = append(arg, "--args")
|
|
|
|
|
for _, nv := range opts.args {
|
|
|
|
|
arg = append(arg, nv[0], nv[1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 21:13:07 +00:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
// If we have a user in the options, use sudo to run "open" as that user
|
|
|
|
|
if opts.user != "" {
|
|
|
|
|
arg = append([]string{"-u", opts.user, "/usr/bin/open"}, arg...)
|
|
|
|
|
cmd = exec.Command("sudo", arg...)
|
|
|
|
|
} else {
|
|
|
|
|
// Otherwise, just run "open" as the current user
|
|
|
|
|
cmd = exec.Command("/usr/bin/open", arg...)
|
|
|
|
|
}
|
2024-10-31 19:24:42 +00:00
|
|
|
tw := &TransientWriter{}
|
|
|
|
|
cmd.Stderr = io.MultiWriter(tw, os.Stderr)
|
|
|
|
|
cmd.Stdout = io.MultiWriter(tw, os.Stdout)
|
2022-04-01 20:28:51 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
2024-10-31 19:24:42 +00:00
|
|
|
return tw.String(), fmt.Errorf("open path %q: %w", path, err)
|
2022-04-01 20:28:51 +00:00
|
|
|
}
|
2024-10-31 19:24:42 +00:00
|
|
|
return tw.String(), nil
|
2022-04-01 20:28:51 +00:00
|
|
|
}
|
2024-11-20 16:44:40 +00:00
|
|
|
|
|
|
|
|
func runWithOutput(path string, opts eopts) (output []byte, exitCode int, err error) {
|
|
|
|
|
return nil, 0, errors.New("not implemented")
|
|
|
|
|
}
|