You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.8 KiB
65 lines
1.8 KiB
package main |
|
|
|
import ( |
|
"log" |
|
"os" |
|
"path" |
|
|
|
"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 { |
|
log.Fatal(err) |
|
} |
|
defer watcher.Close() |
|
|
|
done := make(chan bool) |
|
go func() { |
|
for { |
|
select { |
|
case event := <-watcher.Events: |
|
// 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("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("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() |
|
} |
|
} |
|
}() |
|
|
|
// The configuration file need to be in the same directory as the binary. |
|
// Later on, we can add other paths, where the file can be placed (like as a |
|
// dotfile in the home directory or in the .config/n3rdpad/ folder or other |
|
// 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.") |
|
} |
|
<-done |
|
} |
|
|
|
func main() { |
|
watchForConfigChanges() |
|
stop() |
|
}
|
|
|