-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathexec_darwin.go
74 lines (62 loc) · 2.05 KB
/
exec_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package systray
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
char **makeCharArray(int size) {
return calloc(sizeof(char*), size);
}
void setCharArray(char **a, int n, char *s) {
a[n] = s;
}
void freeCharArray(char **a, int size) {
int i;
for (i = 0; i < size; i++) {
free(a[i]);
}
free(a);
}
void runApplication(const char *path, const char **argv, int argc) {
NSMutableArray<NSString *> *stringArray = [NSMutableArray array];
for (int i=0; i<argc; i++) {
NSString *arg = [NSString stringWithCString:argv[i] encoding:NSUTF8StringEncoding];
[stringArray addObject:arg];
}
NSArray<NSString *> *arguments = [NSArray arrayWithArray:stringArray];
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSURL *url = [NSURL fileURLWithPath:@(path) isDirectory:NO];
NSWorkspaceOpenConfiguration* configuration = [NSWorkspaceOpenConfiguration new];
//[configuration setEnvironment:env];
[configuration setPromptsUserIfNeeded:YES];
[configuration setCreatesNewApplicationInstance:YES];
[configuration setArguments:arguments];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[ws openApplicationAtURL:url configuration:configuration completionHandler:^(NSRunningApplication* app, NSError* error) {
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
*/
import "C"
import (
"os/exec"
"path/filepath"
"github.com/sirupsen/logrus"
)
func execApp(path string, args ...string) error {
if filepath.Ext(path) != ".app" {
// If not .app, fallback to standard process execution
logrus.WithField("path", path).WithField("args", args).Info("Running new app with os/exec.Exec")
cmd := exec.Command(path, args...)
return cmd.Start()
}
logrus.WithField("path", path).WithField("args", args).Info("Running new app with openApplicationAtURL")
argc := C.int(len(args))
argv := C.makeCharArray(argc)
for i, arg := range args {
C.setCharArray(argv, C.int(i), C.CString(arg))
}
C.runApplication(C.CString(path), argv, argc)
C.freeCharArray(argv, argc)
return nil
}