-
Notifications
You must be signed in to change notification settings - Fork 1
/
jettison.lua
75 lines (66 loc) · 2.35 KB
/
jettison.lua
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
75
-- Jettison replacement: Eject ejectable drives on sleep
local logger = hs.logger.new("Jettison")
logger.i("Loading Jettison sleep watcher")
M = {}
log = require('utilities.log').new(logger)
M._jettison_causing_sleep = false
function M.isAnExternalDrivePresent()
local output, status, return_type, return_code =
hs.execute("diskutil list | grep external")
return status == true
end
function M.isAnExternalDriveMounted()
local output, status, return_type, return_code =
hs.execute("for i in $(diskutil list | grep 'external, virtual' | \z
cut -d' ' -f1); do diskutil info $i | \z
grep -q 'Mounted.*Yes' && echo $i; done")
return output ~= ""
end
function M.ejectExternalDrivesAndSleep()
if M._jettison_causing_sleep == true then
log.and_alert("Asked to sleep while Jettison still trying to cause sleep… aborting.")
return nil
end
log.and_alert("Ejecting drives before sleep…")
local output, status, return_type, return_code =
hs.execute("~/code/utilities/Scripts/eject-external-drives")
if status then
log.and_alert("… drives ejected.")
M._jettison_causing_sleep = true
hs.caffeinate.systemSleep()
M._jettison_causing_sleep = false
else
log.warning_and_alert("… but the drives didn't eject: ".. tostring(output) ..
" - return code: " .. tostring(return_code))
end
end
function M.mountExternalDrives()
if M.isAnExternalDrivePresent() then
local output, status, return_type, return_code =
hs.execute("~/code/utilities/Scripts/mount-external-drives")
if status then
log.and_alert("Drives remounted after sleep.")
else
log.warning_and_alert("Drives failed to remount after sleep: "..
tostring(output) .." - return code: " .. tostring(return_code))
end
end
end
function M.sleepWatcherCallback(event)
if (event == hs.caffeinate.watcher.systemWillSleep) and
(not M._jettison_causing_sleep) then
if M.isAnExternalDriveMounted() then
hs.caffeinate.declareUserActivity() -- prevent sleep to give us time to eject drives
M.ejectExternalDrivesAndSleep()
end
elseif event == hs.caffeinate.watcher.systemDidWake then
M.mountExternalDrives()
-- else do nothing
end
end
M.sleepWatcher = hs.caffeinate.watcher.new(M.sleepWatcherCallback)
function M:start()
logger.i("Starting Jettison sleep watcher")
self.sleepWatcher:start()
end
return M