diff --git a/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift b/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift index fa7286d68a5e78c1a20ed50b3d4a372d90a4772d..8e1a19245d2bc4c16a6439e0270bec8733a88798 100644 --- a/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift +++ b/launchers/macosx/I2PLauncher/Utils/LaunchAgent+Status.swift @@ -34,15 +34,15 @@ extension LaunchAgent { /// Run `launchctl start` on the agent /// /// Check the status of the job with `.status()` - public func start() { - LaunchAgentManager.shared.start(self) + public func start(_ callback: ((Process) -> Void)? = nil ) { + LaunchAgentManager.shared.start(self, callback) } /// Run `launchctl stop` on the agent /// /// Check the status of the job with `.status()` - public func stop() { - LaunchAgentManager.shared.stop(self) + public func stop(_ callback: ((Process) -> Void)? = nil ) { + LaunchAgentManager.shared.stop(self, callback) } /// Run `launchctl load` on the agent diff --git a/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift b/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift index 2eb50fe3691444c6330e0ff1b0f9d2a24f081ed4..b9c26c78f891f99d580527cfdd491a372d353baa 100644 --- a/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift +++ b/launchers/macosx/I2PLauncher/Utils/LaunchAgentManager.swift @@ -88,41 +88,53 @@ extension LaunchAgentManager { /// Run `launchctl start` on the agent /// /// Check the status of the job with `.status(_: LaunchAgent)` - public func start(_ agent: LaunchAgent) { + public func start(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) { let arguments = ["start", agent.label] - Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + if ((termHandler) != nil) { + proc.terminationHandler = termHandler + } } /// Run `launchctl stop` on the agent /// /// Check the status of the job with `.status(_: LaunchAgent)` - public func stop(_ agent: LaunchAgent) { + public func stop(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) { let arguments = ["stop", agent.label] - Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + if ((termHandler) != nil) { + proc.terminationHandler = termHandler + } } /// Run `launchctl load` on the agent /// /// Check the status of the job with `.status(_: LaunchAgent)` - public func load(_ agent: LaunchAgent) throws { + public func load(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) throws { guard let agentURL = agent.url else { throw LaunchAgentManagerError.urlNotSet(label: agent.label) } let arguments = ["load", agentURL.path] - Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + if ((termHandler) != nil) { + proc.terminationHandler = termHandler + } } /// Run `launchctl unload` on the agent /// /// Check the status of the job with `.status(_: LaunchAgent)` - public func unload(_ agent: LaunchAgent) throws { + public func unload(_ agent: LaunchAgent, _ termHandler: ((Process) -> Void)? = nil ) throws { guard let agentURL = agent.url else { throw LaunchAgentManagerError.urlNotSet(label: agent.label) } let arguments = ["unload", agentURL.path] - Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + let proc = Process.launchedProcess(launchPath: LaunchAgentManager.launchctl, arguments: arguments) + if ((termHandler) != nil) { + proc.terminationHandler = termHandler + } } /// Retreives the status of the LaunchAgent from `launchctl`