2020-09-03 00:40:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-03 03:56:25 +00:00
|
|
|
"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"
|
2020-09-03 03:56:25 +00:00
|
|
|
"github.com/knadh/koanf/providers/posflag"
|
|
|
|
flag "github.com/spf13/pflag"
|
2020-09-03 04:24:26 +00:00
|
|
|
|
2020-09-05 03:28:01 +00:00
|
|
|
pfconfig "git.kemonine.info/PiFrame/config"
|
2020-09-03 04:24:26 +00:00
|
|
|
"git.kemonine.info/PiFrame/ui"
|
|
|
|
)
|
|
|
|
|
2020-09-03 00:40:28 +00:00
|
|
|
func main() {
|
2020-09-03 03:56:25 +00:00
|
|
|
// Command line flag handler
|
2020-09-03 04:24:26 +00:00
|
|
|
f := flag.NewFlagSet("piframe", flag.ContinueOnError)
|
2020-09-03 03:56:25 +00:00
|
|
|
f.Usage = func() {
|
|
|
|
fmt.Println(f.FlagUsages())
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
// Command line flags
|
2020-09-05 03:28:01 +00:00
|
|
|
f.Bool(pfconfig.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
|
|
|
|
cliFlag := f.Lookup(pfconfig.CLI_FLAG_CONFIG_ONLY)
|
2020-09-03 04:24:26 +00:00
|
|
|
if cliFlag != nil {
|
|
|
|
cliFlag.NoOptDefVal = "true"
|
|
|
|
}
|
2020-09-03 03:56:25 +00:00
|
|
|
// 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{}{
|
2020-09-05 03:28:01 +00:00
|
|
|
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)
|
|
|
|
|
2020-09-03 03:56:25 +00:00
|
|
|
// Bring in /etc/defaults/pf.toml if it exists
|
2020-09-05 03:28:01 +00:00
|
|
|
configFileProvider := file.Provider(pfconfig.CONFIG_FILE_PATH)
|
|
|
|
_, err := os.Stat(pfconfig.CONFIG_FILE_PATH)
|
2020-09-04 01:01:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 04:48:08 +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
|
2020-09-03 05:25:42 +00:00
|
|
|
log.Fatalf("Config file changed! Exiting!")
|
2020-09-03 04:48:08 +00:00
|
|
|
})
|
|
|
|
|
2020-09-03 03:56:25 +00:00
|
|
|
// Process command line flags
|
|
|
|
if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil {
|
2020-09-03 05:25:42 +00:00
|
|
|
log.Fatalf("Error loading command line flags : %s", err)
|
2020-09-03 03:56:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 03:28:01 +00:00
|
|
|
if !pfConfig.Bool(pfconfig.CLI_FLAG_CONFIG_ONLY) {
|
2020-09-04 01:01:13 +00:00
|
|
|
ui.Slideshow(pfConfig)
|
2020-09-03 04:24:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 00:28:32 +00:00
|
|
|
// Reset the CLI flag so it's never writted to the config as 'true'
|
|
|
|
pfConfig.Load(confmap.Provider(map[string]interface{}{
|
2020-09-05 03:28:01 +00:00
|
|
|
pfconfig.CLI_FLAG_CONFIG_ONLY: false,
|
2020-09-05 00:28:32 +00:00
|
|
|
}, "."), nil)
|
|
|
|
|
2020-09-04 01:01:13 +00:00
|
|
|
ui.ConfigGui(pfConfig)
|
2020-09-03 00:40:28 +00:00
|
|
|
}
|