From 57a8200a65daf4e9c2d839c827c92a4bfdca391c Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 12 Jul 2016 23:38:55 +0200 Subject: [PATCH] Implement the watcher correctly --- agent.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/agent.go b/agent.go index be673a1..c7a9957 100644 --- a/agent.go +++ b/agent.go @@ -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() }