Commit ed9fb15 1 parent 504143e commit ed9fb15 Copy full SHA for ed9fb15
File tree 3 files changed +24
-5
lines changed
3 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ struct Watch: ParsableCommand {
11
11
12
12
mutating func run( ) {
13
13
let repo = EventRepo ( )
14
- repo. registerForEventStoreChanges ( )
14
+ let hooks = Loader . readConfig ( ) ? . hooks ?? [ ]
15
+ repo. registerForEventStoreChanges ( hooks: hooks)
15
16
dispatchMain ( )
16
17
}
17
18
}
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
1
3
struct Config : Codable {
2
4
let iconize : [ Rule ] ?
3
5
let hooks : [ Hook ] ?
@@ -12,4 +14,17 @@ struct Rule: Codable {
12
14
struct Hook : Codable {
13
15
let path : String
14
16
let args : [ String ] ?
17
+
18
+ func trigger( ) {
19
+ let task = Process ( )
20
+ task. executableURL = URL ( fileURLWithPath: path)
21
+ task. arguments = args ?? [ ]
22
+ do {
23
+ try task. run ( )
24
+ task. waitUntilExit ( )
25
+ print ( " Fired event \( path) with args \( args ?? [ ] ) " )
26
+ } catch {
27
+ print ( " Failed to execute command \( path) : \( error) " )
28
+ }
29
+ }
15
30
}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import EventKit
2
2
3
3
final class EventRepo {
4
4
private let eventStore = EKEventStore ( )
5
+ private var hooks : [ Hook ] = [ ]
5
6
6
7
private func grantAccess( ) -> EKEventStore {
7
8
if #available( macOS 14 , * ) {
@@ -93,18 +94,20 @@ final class EventRepo {
93
94
StdOut . print ( " Saved Event " )
94
95
}
95
96
96
- /// Register for EKEventStoreChangedNotification
97
- func registerForEventStoreChanges ( ) {
97
+ func registerForEventStoreChanges ( hooks : [ Hook ] ) {
98
+ self . hooks = hooks
98
99
NotificationCenter . default. addObserver (
99
100
self ,
100
101
selector: #selector( eventStoreChanged ( _: ) ) ,
101
102
name: . EKEventStoreChanged,
102
- object: eventStore
103
+ object: nil // Listen to all EKEventStore changes
103
104
)
104
105
}
105
106
106
107
@objc private func eventStoreChanged( _: Notification ) {
107
- print ( " Event store has changed! " )
108
+ for hook in hooks {
109
+ hook. trigger ( )
110
+ }
108
111
}
109
112
110
113
deinit {
You can’t perform that action at this time.
0 commit comments