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-19 22:12:57 +00:00
|
|
|
"time"
|
2020-09-03 03:47:07 +00:00
|
|
|
|
|
|
|
"github.com/knadh/koanf/providers/confmap"
|
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 19:49:49 +00:00
|
|
|
"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-06 01:27:33 +00:00
|
|
|
log.Print("Starting up")
|
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-06 01:27:33 +00:00
|
|
|
log.Print("Setting up CLI flags")
|
2020-09-05 19:49:49 +00:00
|
|
|
f.Bool(config.CLI_FLAG_CONFIG_ONLY, false, "Only show the config UI, NOT the slideshow")
|
|
|
|
cliFlag := f.Lookup(config.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-06 01:27:33 +00:00
|
|
|
log.Print("Loading config")
|
2020-09-05 19:49:49 +00:00
|
|
|
// Load the config file
|
2020-09-19 22:12:57 +00:00
|
|
|
pfConfig, configFileProvider := config.LoadConfig()
|
2020-09-06 01:27:33 +00:00
|
|
|
log.Printf("%v", pfConfig)
|
|
|
|
pfConfig.Print()
|
2020-09-05 21:49:55 +00:00
|
|
|
|
2020-09-03 04:48:08 +00:00
|
|
|
// Watch for config changes and re-load config if needed
|
2020-09-06 07:36:35 +00:00
|
|
|
configFileProvider.Watch(func(event interface{}, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error setting up watch of config : %s", err)
|
|
|
|
return
|
|
|
|
}
|
2020-09-03 04:48:08 +00:00
|
|
|
|
2020-09-06 07:36:35 +00:00
|
|
|
// Give the config UI a chance to save and exit clean
|
|
|
|
time.Sleep(time.Minute)
|
2020-09-05 05:01:48 +00:00
|
|
|
|
2020-09-06 07:36:35 +00:00
|
|
|
// Bail on slideshow if there is a config change so it restarts with updated config
|
|
|
|
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 19:49:49 +00:00
|
|
|
hideSlideshow := pfConfig.Bool(config.CLI_FLAG_CONFIG_ONLY)
|
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 19:49:49 +00:00
|
|
|
config.CLI_FLAG_CONFIG_ONLY: false,
|
2020-09-05 00:28:32 +00:00
|
|
|
}, "."), nil)
|
|
|
|
|
2020-09-05 19:49:49 +00:00
|
|
|
if !hideSlideshow {
|
|
|
|
ui.Slideshow(pfConfig)
|
|
|
|
}
|
|
|
|
|
2020-09-04 01:01:13 +00:00
|
|
|
ui.ConfigGui(pfConfig)
|
2020-09-03 00:40:28 +00:00
|
|
|
}
|