Skip to content

Commit

Permalink
Added better error handling for blockfile and end time
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandoks committed Oct 15, 2023
1 parent 97499a8 commit 96451b1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,30 @@ local function selfControl()
local end_time = os.date("!%Y-%m-%dT%H:%M:%SZ", os.time(os.date("*t")) + block_time * 60)

local selfcontrol_command = "/Applications/SelfControl.app/Contents/MacOS/selfcontrol-cli"
local block_file = hs.fs.pathToAbsolute(block["blocklist"])
local selfcontrol_arguments = {
"start",
"--enddate",
end_time,
"--blocklist",
hs.fs.pathToAbsolute(block["blocklist"]),
block_file,
}

local startSelfControl
local function selfcontrol_callback(exit_code, _, std_error)
if exit_code == 0 then
print("SelfControl started")
elseif string.match(std_error, "Blocklist is empty, or block does not end in the future") then
print("SelfControl does not end in the future")
local block_file_attributes = hs.fs.attributes(block_file)
if not (block_file_attributes and block_file_attributes["mode"] == "file") then
error("Blocklist file " .. block_file .. "does not exist or has an error")
else
error("End date ends in the past")
end
elseif string.match(std_error, "Block is already running") then
print("SelfControl is already running")
error("SelfControl is already running")
elseif string.match(std_error, "Authorization cancelled") then
print("User tried to cancel")
print("User tried to cancel. Restarting...")
startSelfControl()
end
end
Expand All @@ -140,7 +146,7 @@ local function selfControl()
local selfcontrol_task =
hs.task.new(selfcontrol_command, selfcontrol_callback, selfcontrol_arguments)
if not selfcontrol_task:start() then
print("Couldn't start SelfControl task")
error("Couldn't start SelfControl task")
return
end

Expand Down

0 comments on commit 96451b1

Please sign in to comment.