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"
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
|
"git.kemonine.info/PiFrame/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CLI_FLAG_CONFIG_ONLY = "config-ui-only"
|
2020-09-03 03:47:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CONFIG_FILE_PATH = "/etc/default/pf.toml"
|
|
|
|
CONFIG_KEY_SLIDESHOW_DURATION = "slideshow.duration"
|
|
|
|
CONFIG_KEY_HDMI_OFF = "hdmi.off"
|
|
|
|
CONFIG_KEY_HDMI_ON = "hdmi.on"
|
|
|
|
CONFIG_KEY_ALBUMS_ROOT = "albums.root"
|
|
|
|
CONFIG_KEY_ALBUMS_SELECTED = "albums.selected"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DEFAULT_SLIDESHOW_DURATION = "300s"
|
|
|
|
DEFAULT_HDMI_OFF = "*-*-* 00:00:00"
|
|
|
|
DEFAULT_HDMI_ON = "*-*-* 06:00:00"
|
|
|
|
DEFAULT_ALBUMS_ROOT = "/tank/pictures"
|
|
|
|
DEFAULT_ALBUM_SELECTED = "/"
|
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-03 04:24:26 +00:00
|
|
|
f.Bool(CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
|
|
|
|
cliFlag := f.Lookup(CLI_FLAG_CONFIG_ONLY)
|
|
|
|
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{}{
|
|
|
|
CONFIG_KEY_SLIDESHOW_DURATION: DEFAULT_SLIDESHOW_DURATION,
|
|
|
|
CONFIG_KEY_HDMI_OFF: DEFAULT_HDMI_OFF,
|
|
|
|
CONFIG_KEY_HDMI_ON: DEFAULT_HDMI_ON,
|
|
|
|
CONFIG_KEY_ALBUMS_ROOT: DEFAULT_ALBUMS_ROOT,
|
|
|
|
CONFIG_KEY_ALBUMS_SELECTED: []string{DEFAULT_ALBUM_SELECTED},
|
|
|
|
}, "."), nil)
|
|
|
|
|
2020-09-03 03:56:25 +00:00
|
|
|
// Bring in /etc/defaults/pf.toml if it exists
|
2020-09-03 04:48:08 +00:00
|
|
|
configFileProvider := file.Provider(CONFIG_FILE_PATH)
|
2020-09-03 03:47:07 +00:00
|
|
|
_, err := os.Stat(CONFIG_FILE_PATH)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-09-03 04:48:08 +00:00
|
|
|
// log.Printf("%s does not exist, USING DEFAULTS", CONFIG_FILE_PATH)
|
2020-09-03 03:47:07 +00:00
|
|
|
} else {
|
2020-09-03 04:48:08 +00:00
|
|
|
if errConfigFile := pfConfig.Load(configFileProvider, toml.Parser()); errConfigFile != nil {
|
2020-09-03 03:56:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pfConfig.Load(configFileProvider, toml.Parser())
|
|
|
|
})
|
|
|
|
|
2020-09-03 03:56:25 +00:00
|
|
|
// Process command line flags
|
|
|
|
if err := pfConfig.Load(posflag.Provider(f, ".", pfConfig), nil); err != nil {
|
|
|
|
log.Fatalf("Error loading command line flags : %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-03 04:24:26 +00:00
|
|
|
if !pfConfig.Bool(CLI_FLAG_CONFIG_ONLY) {
|
|
|
|
ui.Slideshow()
|
|
|
|
}
|
|
|
|
|
2020-09-03 04:48:08 +00:00
|
|
|
ui.ConfigGui()
|
2020-09-03 00:40:28 +00:00
|
|
|
}
|