/
eject_darwin.go
50 lines (43 loc) · 965 Bytes
/
eject_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
package eject
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation -framework DiscRecording
#import <Foundation/Foundation.h>
#import <DiscRecording/DiscRecording.h>
static int
_eject() {
@autoreleasepool {
NSArray* devices = [DRDevice devices];
if ([devices count] < 1) {
return -1;
}
DRDevice* firstDevice = devices[0];
NSDictionary* status = [firstDevice status];
BOOL ok;
if ([(NSNumber*)status[DRDeviceIsTrayOpenKey] intValue] == 1) {
ok = [firstDevice closeTray];
}
else {
ok = [firstDevice openTray];
}
if (ok) {
return 0;
}
else {
return -2;
}
}
}
*/
import "C"
import "errors"
func Eject() error {
if r := C._eject(); r != 0 {
if r == -1 {
return errors.New("drive not found")
} else {
return errors.New("failed to eject")
}
}
return nil
}