1 | // |
---|
2 | // LaunchAgent+Status.swift |
---|
3 | // I2PLauncher |
---|
4 | // |
---|
5 | // Created by Mikal Villa on 05/10/2018. |
---|
6 | // Copyright © 2018 The I2P Project. All rights reserved. |
---|
7 | // |
---|
8 | |
---|
9 | import Foundation |
---|
10 | |
---|
11 | public enum AgentStatus: Equatable { |
---|
12 | |
---|
13 | case running(pid: Int) |
---|
14 | case loaded |
---|
15 | case unloaded |
---|
16 | |
---|
17 | public static func ==(lhs: AgentStatus, rhs: AgentStatus) -> Bool { |
---|
18 | switch (lhs, rhs) { |
---|
19 | case ( let .running(lhpid), let .running(rhpid) ): |
---|
20 | return lhpid == rhpid |
---|
21 | case (.loaded, .loaded): |
---|
22 | return true |
---|
23 | case (.unloaded, .unloaded): |
---|
24 | return true |
---|
25 | default: |
---|
26 | return false |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | extension LaunchAgent { |
---|
33 | |
---|
34 | /// Run `launchctl start` on the agent |
---|
35 | /// |
---|
36 | /// Check the status of the job with `.status()` |
---|
37 | public func start(_ callback: ((Process) -> Void)? = nil ) { |
---|
38 | LaunchAgentManager.shared.start(self, callback) |
---|
39 | } |
---|
40 | |
---|
41 | /// Run `launchctl stop` on the agent |
---|
42 | /// |
---|
43 | /// Check the status of the job with `.status()` |
---|
44 | public func stop(_ callback: ((Process) -> Void)? = nil ) { |
---|
45 | LaunchAgentManager.shared.stop(self, callback) |
---|
46 | } |
---|
47 | |
---|
48 | /// Run `launchctl load` on the agent |
---|
49 | /// |
---|
50 | /// Check the status of the job with `.status()` |
---|
51 | public func load() throws { |
---|
52 | try LaunchAgentManager.shared.load(self) |
---|
53 | } |
---|
54 | |
---|
55 | /// Run `launchctl unload` on the agent |
---|
56 | /// |
---|
57 | /// Check the status of the job with `.status()` |
---|
58 | public func unload() throws { |
---|
59 | try LaunchAgentManager.shared.unload(self) |
---|
60 | } |
---|
61 | |
---|
62 | /// Retreives the status of the LaunchAgent from `launchctl` |
---|
63 | /// |
---|
64 | /// - Returns: the agent's status |
---|
65 | public func status() -> AgentStatus { |
---|
66 | return LaunchAgentManager.shared.status(self) |
---|
67 | } |
---|
68 | |
---|
69 | } |
---|