Implement the watcher correctly

This commit is contained in:
Aaron Fischer 2016-07-12 23:38:55 +02:00
parent e6bff5a4a4
commit 57a8200a65

View file

@ -1,7 +1,6 @@
package main
import (
"fmt"
"log"
"os"
"path"
@ -9,6 +8,12 @@ import (
"github.com/fsnotify/fsnotify"
)
func stop() {
log.Println("Have a nice day.")
// TODO: Make the error code optional (as a parameter)
os.Exit(1)
}
func watchForConfigChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
@ -21,12 +26,22 @@ func watchForConfigChanges() {
for {
select {
case event := <-watcher.Events:
log.Println("event: ", event)
// The file is changed. This is the only event we are interested
// in. If the file is renamed, removed or something else, we drop
// an error to the user.
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
log.Println("Reload the config file ...")
}
if event.Op&fsnotify.Rename == fsnotify.Rename ||
event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("The 'keymap.conf' configuration file is renamed or removed. We cant do any further changes without the config file. So create the file or rename it back and restart the agent.")
stop()
}
case err := <-watcher.Errors:
log.Println("error:", err)
log.Println("We have the following problem with the 'keymap.conf' configuration file: ", err)
log.Println("Try the fix this problem by yourself and restart the agent.")
stop()
}
}
}()
@ -37,7 +52,6 @@ func watchForConfigChanges() {
// places where windows will put it).
config_path, err := os.Getwd()
config_file := path.Join(config_path, "keymap.conf")
err = watcher.Add(config_file)
if err != nil {
log.Fatal("Can't find the 'keymap.conf' configuration file. Please create one.")
@ -46,6 +60,6 @@ func watchForConfigChanges() {
}
func main() {
fmt.Printf("Ready\n")
watchForConfigChanges()
stop()
}