74 lines
2.7 KiB
Swift
74 lines
2.7 KiB
Swift
|
|
import Foundation
|
||
|
|
import NotifierCore
|
||
|
|
|
||
|
|
// A hook must never block or fail luxe: always exit 0, print nothing on stdout, report on stderr.
|
||
|
|
func warn(_ message: String) {
|
||
|
|
FileHandle.standardError.write(Data("luxe-notify: \(message)\n".utf8))
|
||
|
|
}
|
||
|
|
|
||
|
|
struct Options {
|
||
|
|
var hookPoint = ""
|
||
|
|
var socketPath = NotifySocket.defaultPath()
|
||
|
|
var minimumSeconds = TurnTimer.defaultMinimumSeconds
|
||
|
|
|
||
|
|
/// Flags may appear anywhere; the first bare word is the hook point.
|
||
|
|
static func parse(_ arguments: [String]) -> Options? {
|
||
|
|
var options = Options()
|
||
|
|
var index = 0
|
||
|
|
var sawHookPoint = false
|
||
|
|
while index < arguments.count {
|
||
|
|
let argument = arguments[index]
|
||
|
|
switch argument {
|
||
|
|
case "--min-seconds":
|
||
|
|
guard index + 1 < arguments.count, let value = TimeInterval(arguments[index + 1]) else {
|
||
|
|
warn("--min-seconds needs a number")
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
options.minimumSeconds = value
|
||
|
|
index += 1
|
||
|
|
case "--socket":
|
||
|
|
guard index + 1 < arguments.count else { warn("--socket needs a path"); return nil }
|
||
|
|
options.socketPath = arguments[index + 1]
|
||
|
|
index += 1
|
||
|
|
default:
|
||
|
|
if !sawHookPoint { options.hookPoint = argument; sawHookPoint = true }
|
||
|
|
}
|
||
|
|
index += 1
|
||
|
|
}
|
||
|
|
return sawHookPoint ? options : nil
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func isTurnStart(_ hookPoint: String) -> Bool {
|
||
|
|
hookPoint.lowercased().replacingOccurrences(of: "_", with: "") == "beforeagentstart"
|
||
|
|
}
|
||
|
|
|
||
|
|
func isTurnEnd(_ hookPoint: String) -> Bool {
|
||
|
|
hookPoint.lowercased().replacingOccurrences(of: "_", with: "") == "agentend"
|
||
|
|
}
|
||
|
|
|
||
|
|
guard let options = Options.parse(Array(CommandLine.arguments.dropFirst())) else {
|
||
|
|
warn("usage: luxe-notify <HookPoint> [--min-seconds N] [--socket PATH] (event JSON on stdin)")
|
||
|
|
exit(0)
|
||
|
|
}
|
||
|
|
|
||
|
|
let input = FileHandle.standardInput.readDataToEndOfFile()
|
||
|
|
let appDirectory = (NotifySocket.defaultPath() as NSString).deletingLastPathComponent
|
||
|
|
|
||
|
|
do {
|
||
|
|
if isTurnStart(options.hookPoint) {
|
||
|
|
try TurnTimer.recordStart(at: Date(), directory: appDirectory)
|
||
|
|
exit(0)
|
||
|
|
}
|
||
|
|
if isTurnEnd(options.hookPoint) {
|
||
|
|
let started = TurnTimer.readStart(directory: appDirectory)
|
||
|
|
guard TurnTimer.shouldNotify(startedAt: started, endedAt: Date(), minimumSeconds: options.minimumSeconds) else { exit(0) }
|
||
|
|
}
|
||
|
|
var event = try HookEventParser.parse(input, hookPoint: options.hookPoint, cwd: FileManager.default.currentDirectoryPath)
|
||
|
|
event.terminalBundleID = TerminalLocator.bundleID(from: ProcessInfo.processInfo.environment)
|
||
|
|
try NotifyClient.send(event, to: options.socketPath)
|
||
|
|
} catch {
|
||
|
|
warn("\(error)")
|
||
|
|
}
|
||
|
|
exit(0)
|