97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/mkideal/cli"
|
|
)
|
|
|
|
func stop() {
|
|
log.Println("Have a nice day.")
|
|
// TODO: Make the error code optional (as a parameter)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func waitAndExecuteCommandsFromDevice() {
|
|
// TODO: Establish connection to the device
|
|
// TODO: Wait for action loop
|
|
// TODO: Execute action
|
|
// TODO: Wait for action loop ...
|
|
}
|
|
|
|
func watchForConfigChanges(configFile string) {
|
|
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 ...")
|
|
// TODO: Parse the config file
|
|
// TODO: Handle errors in the config file
|
|
// TODO: Write a mapping for the mapping of the keys to the binary format the avr wants.
|
|
// TODO: Establish a connection to the device
|
|
// TODO: Transfer the new key mappings
|
|
// TODO: Close the connection?
|
|
}
|
|
|
|
if event.Op&fsnotify.Rename == fsnotify.Rename ||
|
|
event.Op&fsnotify.Remove == fsnotify.Remove {
|
|
log.Println("The 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 configuration file: ", err)
|
|
log.Println("Try the fix this problem by yourself and restart the agent.")
|
|
stop()
|
|
}
|
|
}
|
|
}()
|
|
|
|
err = watcher.Add(configFile)
|
|
if err != nil {
|
|
log.Fatal("Can't find the configuration file. Please create one.")
|
|
}
|
|
<-done
|
|
}
|
|
|
|
type argT struct {
|
|
cli.Helper
|
|
ConfigFile string `cli:"c,config" usage:"specify a keymap config file"`
|
|
}
|
|
|
|
func main() {
|
|
configPath, _ := os.Getwd()
|
|
configFile := path.Join(configPath, "keymap.conf")
|
|
|
|
cli.Run(&argT{}, func(ctx *cli.Context) error {
|
|
args := ctx.Argv().(*argT)
|
|
if args.ConfigFile != "" {
|
|
configFile = args.ConfigFile
|
|
}
|
|
|
|
if !args.Help {
|
|
// Start up the application
|
|
log.Println("Using the configuration file", configFile)
|
|
watchForConfigChanges(configFile)
|
|
waitAndExecuteCommandsFromDevice()
|
|
|
|
stop()
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|