Add watching of config file to re-load if changed on disk while running

This commit is contained in:
KemoNine 2020-09-03 00:48:08 -04:00
parent ce9f53cb45
commit 8b3827220e

View file

@ -47,7 +47,6 @@ func main() {
f.Bool(CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow") f.Bool(CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
cliFlag := f.Lookup(CLI_FLAG_CONFIG_ONLY) cliFlag := f.Lookup(CLI_FLAG_CONFIG_ONLY)
if cliFlag != nil { if cliFlag != nil {
log.Print("Could not find CLI_FLAG_CONFIG_ONLY")
cliFlag.NoOptDefVal = "true" cliFlag.NoOptDefVal = "true"
} }
// Process command line flags into handler // Process command line flags into handler
@ -66,31 +65,36 @@ func main() {
}, "."), nil) }, "."), nil)
// Bring in /etc/defaults/pf.toml if it exists // Bring in /etc/defaults/pf.toml if it exists
configFileProvider := file.Provider(CONFIG_FILE_PATH)
_, err := os.Stat(CONFIG_FILE_PATH) _, err := os.Stat(CONFIG_FILE_PATH)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
log.Printf("%s does not exist, USING DEFAULTS", CONFIG_FILE_PATH) // log.Printf("%s does not exist, USING DEFAULTS", CONFIG_FILE_PATH)
} else { } else {
if errConfigFile := pfConfig.Load(file.Provider(CONFIG_FILE_PATH), toml.Parser()); errConfigFile != nil { if errConfigFile := pfConfig.Load(configFileProvider, toml.Parser()); errConfigFile != nil {
log.Fatalf("Error loading config : %s", err) log.Fatalf("Error loading config : %s", err)
} }
} }
} }
// Watch for config changes and re-load config if needed
configFileProvider.Watch(func(event interface{}, err error) {
if err != nil {
log.Printf("Error setting up watch of config : %s", err)
return
}
pfConfig.Load(configFileProvider, toml.Parser())
})
// Process command line flags // Process command line flags
if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil { if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil {
log.Fatalf("Error loading command line flags : %v", err) log.Fatalf("Error loading command line flags : %v", err)
} }
// slideshowDuration := pfConfig.Duration(CONFIG_KEY_SLIDESHOW_DURATION)
// hdmiOff := pfConfig.String(CONFIG_KEY_HDMI_OFF)
// hdmiOn := pfConfig.String(CONFIG_KEY_HDMI_ON)
// albumsRoot := pfConfig.String(CONFIG_KEY_ALBUMS_ROOT)
// albumsSelected := pfConfig.Strings(CONFIG_KEY_ALBUMS_SELECTED)
if !pfConfig.Bool(CLI_FLAG_CONFIG_ONLY) { if !pfConfig.Bool(CLI_FLAG_CONFIG_ONLY) {
ui.Slideshow() ui.Slideshow()
} }
//ui.ConfigGui() ui.ConfigGui()
} }