mirror of
https://github.com/simon987/beemer.git
synced 2025-04-19 16:16:41 +00:00
refactoring
This commit is contained in:
parent
230a2cba59
commit
50f6b69b40
62
main.go
62
main.go
@ -78,7 +78,7 @@ func main() {
|
|||||||
|
|
||||||
defer watcher.Close()
|
defer watcher.Close()
|
||||||
|
|
||||||
go handleFileChange(watcher)
|
go handleWatcherEvents(watcher)
|
||||||
|
|
||||||
logrus.WithField("dir", watchDir).Info("Watching directory for changes")
|
logrus.WithField("dir", watchDir).Info("Watching directory for changes")
|
||||||
err = watcher.Add(watchDir)
|
err = watcher.Add(watchDir)
|
||||||
@ -118,40 +118,55 @@ func getAndResetTimer(name string) *time.Timer {
|
|||||||
return newTimer
|
return newTimer
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFileChange(watcher *fsnotify.Watcher) {
|
func isDir(name string) bool {
|
||||||
for {
|
if stat, err := os.Stat(name); err == nil && stat.IsDir() {
|
||||||
select {
|
return true
|
||||||
case event, ok := <-watcher.Events:
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleDirChange(event fsnotify.Event, watcher *fsnotify.Watcher) error {
|
||||||
|
|
||||||
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
|
return watcher.Add(event.Name)
|
||||||
|
} else if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
|
||||||
|
return watcher.Remove(event.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleFileChange(event fsnotify.Event) {
|
||||||
|
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
if stat, err := os.Stat(event.Name); err == nil && stat.IsDir() {
|
|
||||||
logrus.WithField("name", event.Name).Info("Created dir")
|
|
||||||
err = watcher.Add(event.Name)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t := getAndResetTimer(event.Name)
|
t := getAndResetTimer(event.Name)
|
||||||
if t != nil {
|
if t != nil {
|
||||||
go handleFileInactive(t, event.Name)
|
go handleFileInactive(t, event.Name)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
|
} else if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
|
||||||
if stat, err := os.Stat(event.Name); err == nil && stat.IsDir() {
|
if file, ok := ctx.FileMap[event.Name]; ok {
|
||||||
logrus.WithField("name", event.Name).Info("Removed dir")
|
|
||||||
err = watcher.Remove(event.Name)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
} else if file, ok := ctx.FileMap[event.Name]; ok {
|
|
||||||
file.WaitTimer.Stop()
|
file.WaitTimer.Stop()
|
||||||
delete(ctx.FileMap, event.Name)
|
delete(ctx.FileMap, event.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleWatcherEvents(watcher *fsnotify.Watcher) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event, ok := <-watcher.Events:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isDir(event.Name) {
|
||||||
|
err := handleDirChange(event, watcher)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
handleFileChange(event)
|
||||||
|
}
|
||||||
|
|
||||||
if event.Op&fsnotify.Chmod != fsnotify.Chmod {
|
if event.Op&fsnotify.Chmod != fsnotify.Chmod {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
@ -164,6 +179,7 @@ func handleFileChange(watcher *fsnotify.Watcher) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.WithError(err).Error("error with Watcher")
|
logrus.WithError(err).Error("error with Watcher")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user