File tree Expand file tree Collapse file tree 3 files changed +24
-5
lines changed
Expand file tree Collapse file tree 3 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ struct Watch: ParsableCommand {
1111
1212 mutating func run( ) {
1313 let repo = EventRepo ( )
14- repo. registerForEventStoreChanges ( )
14+ let hooks = Loader . readConfig ( ) ? . hooks ?? [ ]
15+ repo. registerForEventStoreChanges ( hooks: hooks)
1516 dispatchMain ( )
1617 }
1718}
Original file line number Diff line number Diff line change 1+ import Foundation
2+
13struct Config : Codable {
24 let iconize : [ Rule ] ?
35 let hooks : [ Hook ] ?
@@ -12,4 +14,17 @@ struct Rule: Codable {
1214struct Hook : Codable {
1315 let path : String
1416 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+ }
1530}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import EventKit
22
33final class EventRepo {
44 private let eventStore = EKEventStore ( )
5+ private var hooks : [ Hook ] = [ ]
56
67 private func grantAccess( ) -> EKEventStore {
78 if #available( macOS 14 , * ) {
@@ -93,18 +94,20 @@ final class EventRepo {
9394 StdOut . print ( " Saved Event " )
9495 }
9596
96- /// Register for EKEventStoreChangedNotification
97- func registerForEventStoreChanges ( ) {
97+ func registerForEventStoreChanges ( hooks : [ Hook ] ) {
98+ self . hooks = hooks
9899 NotificationCenter . default. addObserver (
99100 self ,
100101 selector: #selector( eventStoreChanged ( _: ) ) ,
101102 name: . EKEventStoreChanged,
102- object: eventStore
103+ object: nil // Listen to all EKEventStore changes
103104 )
104105 }
105106
106107 @objc private func eventStoreChanged( _: Notification ) {
107- print ( " Event store has changed! " )
108+ for hook in hooks {
109+ hook. trigger ( )
110+ }
108111 }
109112
110113 deinit {
You can’t perform that action at this time.
0 commit comments