diff --git a/agent.go b/agent.go index 23031ac..492ce5b 100644 --- a/agent.go +++ b/agent.go @@ -6,6 +6,7 @@ import ( "path" "github.com/fsnotify/fsnotify" + "github.com/mkideal/cli" ) func stop() { @@ -21,7 +22,7 @@ func waitAndExecuteCommandsFromDevice() { // TODO: Wait for action loop ... } -func watchForConfigChanges() { +func watchForConfigChanges(configFile string) { watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) @@ -48,32 +49,48 @@ func watchForConfigChanges() { 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.") + 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 'keymap.conf' configuration file: ", err) + 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() } } }() - // 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) + err = watcher.Add(configFile) if err != nil { - log.Fatal("Can't find the 'keymap.conf' configuration file. Please create one.") + log.Fatal("Can't find the configuration file. Please create one.") } <-done } -func main() { - watchForConfigChanges() - waitAndExecuteCommandsFromDevice() - stop() +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 + }) }