Implement the watcher correctly
This commit is contained in:
parent
e6bff5a4a4
commit
57a8200a65
1 changed files with 20 additions and 6 deletions
26
agent.go
26
agent.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -9,6 +8,12 @@ import (
|
||||||
"github.com/fsnotify/fsnotify"
|
"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() {
|
func watchForConfigChanges() {
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -21,12 +26,22 @@ func watchForConfigChanges() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-watcher.Events:
|
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 {
|
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:
|
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).
|
// places where windows will put it).
|
||||||
config_path, err := os.Getwd()
|
config_path, err := os.Getwd()
|
||||||
config_file := path.Join(config_path, "keymap.conf")
|
config_file := path.Join(config_path, "keymap.conf")
|
||||||
|
|
||||||
err = watcher.Add(config_file)
|
err = watcher.Add(config_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Can't find the 'keymap.conf' configuration file. Please create one.")
|
log.Fatal("Can't find the 'keymap.conf' configuration file. Please create one.")
|
||||||
|
@ -46,6 +60,6 @@ func watchForConfigChanges() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("Ready\n")
|
|
||||||
watchForConfigChanges()
|
watchForConfigChanges()
|
||||||
|
stop()
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue