add: [filerwatcher] notify watcher routine

master
Jean-Louis Huynen 2021-02-16 19:10:36 +01:00
parent 36d7e16bd2
commit fbb4abac22
No known key found for this signature in database
GPG Key ID: 64799157F4BD6B93
1 changed files with 9 additions and 5 deletions

View File

@ -33,20 +33,24 @@ func NewFileWatcherReader(f *os.File, j bool) (*FileWatcherReader, error) {
eic: make(chan notify.EventInfo, 1), eic: make(chan notify.EventInfo, 1),
json: j, json: j,
} }
// go routine holding the watcher
go func() {
if err := notify.Watch("./...", r.eic, notify.InCloseWrite); err != nil {
log.Fatal(err)
}
defer notify.Stop(r.eic)
<-r.exit
}()
return r, nil return r, nil
} }
// Read waits for InCloseWrite file event uses a bytes reader to copy // Read waits for InCloseWrite file event uses a bytes reader to copy
// the resulting file encoded in b64 in p // the resulting file encoded in b64 in p
func (fw *FileWatcherReader) Read(p []byte) (n int, err error) { func (fw *FileWatcherReader) Read(p []byte) (n int, err error) {
if err := notify.Watch("./...", fw.eic, notify.InCloseWrite); err != nil {
log.Fatal(err)
}
defer notify.Stop(fw.eic)
for{ for{
select{ select{
case ei := <-fw.eic: case ei := <-fw.eic:
log.Println("Got event:", ei)
// New File, let's read its content // New File, let's read its content
var err error var err error
fw.buf, err = ioutil.ReadFile(ei.Path()) fw.buf, err = ioutil.ReadFile(ei.Path())