From 142e912d6a0583baebf081e567f072667967fb8c Mon Sep 17 00:00:00 2001 From: Ivan Pesin Date: Tue, 12 Sep 2017 00:10:16 -0500 Subject: [PATCH] Fix to support hot reload with vim editor changes --- webhook.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/webhook.go b/webhook.go index 1034290c..e1396e80 100644 --- a/webhook.go +++ b/webhook.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "strings" + "time" "github.com/adnanh/webhook/hook" @@ -427,10 +428,13 @@ func watchForFileChange() { log.Printf("hooks file %s modified\n", event.Name) reloadHooks(event.Name) } else if event.Op&fsnotify.Remove == fsnotify.Remove { - log.Printf("hooks file %s removed, no longer watching this file for changes, removing hooks that were loaded from it\n", event.Name) - (*watcher).Remove(event.Name) - removeHooks(event.Name) + if _, err := os.Stat(event.Name); os.IsNotExist(err) { + log.Printf("hooks file %s removed, no longer watching this file for changes, removing hooks that were loaded from it\n", event.Name) + (*watcher).Remove(event.Name) + removeHooks(event.Name) + } } else if event.Op&fsnotify.Rename == fsnotify.Rename { + time.Sleep(100 * time.Millisecond) if _, err := os.Stat(event.Name); os.IsNotExist(err) { // file was removed log.Printf("hooks file %s removed, no longer watching this file for changes, and removing hooks that were loaded from it\n", event.Name) @@ -440,6 +444,8 @@ func watchForFileChange() { // file was overwritten log.Printf("hooks file %s overwritten\n", event.Name) reloadHooks(event.Name) + (*watcher).Remove(event.Name) + (*watcher).Add(event.Name) } } case err := <-(*watcher).Errors: