Add commandline options

This commit is contained in:
Aaron Fischer 2016-07-13 20:56:01 +02:00
parent 430ba6d062
commit 05c15ab624

View file

@ -6,6 +6,7 @@ import (
"path" "path"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/mkideal/cli"
) )
func stop() { func stop() {
@ -21,7 +22,7 @@ func waitAndExecuteCommandsFromDevice() {
// TODO: Wait for action loop ... // TODO: Wait for action loop ...
} }
func watchForConfigChanges() { func watchForConfigChanges(configFile string) {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -48,32 +49,48 @@ func watchForConfigChanges() {
if event.Op&fsnotify.Rename == fsnotify.Rename || if event.Op&fsnotify.Rename == fsnotify.Rename ||
event.Op&fsnotify.Remove == fsnotify.Remove { 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() stop()
} }
case err := <-watcher.Errors: 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.") log.Println("Try the fix this problem by yourself and restart the agent.")
stop() stop()
} }
} }
}() }()
// The configuration file need to be in the same directory as the binary. err = watcher.Add(configFile)
// 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 { 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 <-done
} }
type argT struct {
cli.Helper
ConfigFile string `cli:"c,config" usage:"specify a keymap config file"`
}
func main() { func main() {
watchForConfigChanges() 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() waitAndExecuteCommandsFromDevice()
stop() stop()
} }
return nil
})
}