piframe-go/cmd/gui/gui.go

90 lines
2.8 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
2020-09-03 03:47:07 +00:00
"log"
"os"
2020-09-05 05:01:48 +00:00
"time"
2020-09-03 03:47:07 +00:00
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
flag "github.com/spf13/pflag"
pfconfig "git.kemonine.info/PiFrame/config"
"git.kemonine.info/PiFrame/ui"
)
func main() {
// Command line flag handler
f := flag.NewFlagSet("piframe", flag.ContinueOnError)
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
// Command line flags
f.Bool(pfconfig.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
cliFlag := f.Lookup(pfconfig.CLI_FLAG_CONFIG_ONLY)
if cliFlag != nil {
cliFlag.NoOptDefVal = "true"
}
// Process command line flags into handler
f.Parse(os.Args[1:])
2020-09-03 03:47:07 +00:00
// Main config variable
var pfConfig = koanf.New(".")
// Setup some defaults
pfConfig.Load(confmap.Provider(map[string]interface{}{
pfconfig.CONFIG_KEY_SLIDESHOW_INTERVAL: pfconfig.DEFAULT_SLIDESHOW_INTERVAL,
pfconfig.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL: pfconfig.DEFAULT_SLIDESHOW_RESTART_INTERVAL,
pfconfig.CONFIG_KEY_HDMI_OFF: pfconfig.DEFAULT_HDMI_OFF,
pfconfig.CONFIG_KEY_HDMI_ON: pfconfig.DEFAULT_HDMI_ON,
pfconfig.CONFIG_KEY_ALBUMS_ROOT: pfconfig.DEFAULT_ALBUMS_ROOT,
pfconfig.CONFIG_KEY_ALBUMS_SELECTED: []string{pfconfig.DEFAULT_ALBUM_SELECTED},
2020-09-03 03:47:07 +00:00
}, "."), nil)
// Bring in /etc/defaults/pf.toml if it exists
configFileProvider := file.Provider(pfconfig.CONFIG_FILE_PATH)
_, err := os.Stat(pfconfig.CONFIG_FILE_PATH)
if os.IsNotExist(err) {
//log.Printf("%s does not exist, USING DEFAULTS", ui.CONFIG_FILE_PATH)
} else {
if errConfigFile := pfConfig.Load(configFileProvider, toml.Parser()); errConfigFile != nil {
log.Fatalf("Error loading config : %s", err)
2020-09-03 03:47:07 +00:00
}
}
// 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
}
2020-09-05 05:01:48 +00:00
// Give the config UI a chance to save and exit clean
time.Sleep(time.Minute)
// Bail on slideshow if there is a config change so it restarts with updated config
log.Fatalf("Config file changed! Exiting!")
})
// Process command line flags
if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil {
log.Fatalf("Error loading command line flags : %s", err)
}
if !pfConfig.Bool(pfconfig.CLI_FLAG_CONFIG_ONLY) {
ui.Slideshow(pfConfig)
}
// Reset the CLI flag so it's never writted to the config as 'true'
pfConfig.Load(confmap.Provider(map[string]interface{}{
pfconfig.CLI_FLAG_CONFIG_ONLY: false,
}, "."), nil)
ui.ConfigGui(pfConfig)
}