()
| 64 | } |
| 65 | |
| 66 | func WatchComDB() (bool, error) { |
| 67 | functionName := "WatchComDB" |
| 68 | |
| 69 | // Check if bus file already exists |
| 70 | _, existErr := os.Stat(auxiliary.StateInstance.ComDBPath) |
| 71 | if errors.Is(existErr, os.ErrNotExist) { |
| 72 | // Create file if it doesn't exist yet |
| 73 | if _, fileMkErr := os.Create(auxiliary.StateInstance.ComDBPath); fileMkErr != nil { |
| 74 | return false, fmt.Errorf("%v: %w", functionName, fileMkErr) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Define file watcher |
| 79 | watcherInstance, watchErr := fsnotify.NewWatcher() |
| 80 | if watchErr != nil { |
| 81 | return false, fmt.Errorf("%v: %w", functionName, watchErr) |
| 82 | } |
| 83 | defer watcherInstance.Close() |
| 84 | |
| 85 | // Add bus file to watcher |
| 86 | addErr := watcherInstance.Add(auxiliary.StateInstance.ComDBPath) |
| 87 | if addErr != nil { |
| 88 | return false, fmt.Errorf("%v: %w", functionName, addErr) |
| 89 | } |
| 90 | |
| 91 | // Handle watcher events |
| 92 | for { |
| 93 | select { |
| 94 | case event := <-watcherInstance.Events: |
| 95 | // Switch different watcher event types |
| 96 | switch { |
| 97 | // If a write event happens |
| 98 | case event.Op&fsnotify.Write == fsnotify.Write: |
| 99 | status, processErr := ProcessEvents() |
| 100 | if processErr != nil { |
| 101 | return false, fmt.Errorf("processing events failed -> %v: %w", functionName, processErr) |
| 102 | } |
| 103 | |
| 104 | if status == "confirmed" { |
| 105 | return true, nil |
| 106 | } |
| 107 | |
| 108 | if status == "canceled" { |
| 109 | return false, nil |
| 110 | } |
| 111 | } |
| 112 | // Handle watcher error |
| 113 | case err := <-watcherInstance.Errors: |
| 114 | return false, fmt.Errorf("%v: %w", functionName, err) |
| 115 | } |
| 116 | } |
| 117 | } |
nothing calls this directly
no test coverage detected