piframe-go/cmd/gui/gui.go

85 lines
2.6 KiB
Go

package main
import (
"fmt"
"log"
"os"
"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:])
// 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},
}, "."), 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)
}
}
// 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
}
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)
}