From ee03bde2dee3d412ebf1fc24b3745b260152e4b9 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 21 Dec 2019 15:57:51 +0100 Subject: [PATCH] Add auto-reload on config change Signed-off-by: Knut Ahlers --- cmd/streamdeck/config.go | 7 +++++++ cmd/streamdeck/go.mod | 1 + cmd/streamdeck/go.sum | 2 ++ cmd/streamdeck/main.go | 40 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/cmd/streamdeck/config.go b/cmd/streamdeck/config.go index c0d17df..0354771 100644 --- a/cmd/streamdeck/config.go +++ b/cmd/streamdeck/config.go @@ -3,6 +3,7 @@ package main import "time" type config struct { + AutoReload bool `yaml:"auto_reload"` DefaultBrightness int `yaml:"default_brightness"` DefaultPage string `yaml:"default_page"` DisplayOffTime time.Duration `yaml:"display_off_time"` @@ -24,3 +25,9 @@ type dynamicElement struct { Type string `yaml:"type"` Attributes map[string]interface{} `yaml:"attributes"` } + +func newConfig() config { + return config{ + AutoReload: true, + } +} diff --git a/cmd/streamdeck/go.mod b/cmd/streamdeck/go.mod index c6967ff..5035c0b 100644 --- a/cmd/streamdeck/go.mod +++ b/cmd/streamdeck/go.mod @@ -7,6 +7,7 @@ replace github.com/Luzifer/streamdeck => ../../ require ( github.com/Luzifer/rconfig/v2 v2.2.1 github.com/Luzifer/streamdeck v0.0.0-20191122003228-a2f524a6b22c + github.com/fsnotify/fsnotify v1.4.7 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/pkg/errors v0.8.1 diff --git a/cmd/streamdeck/go.sum b/cmd/streamdeck/go.sum index a845bd7..d6966bc 100644 --- a/cmd/streamdeck/go.sum +++ b/cmd/streamdeck/go.sum @@ -3,6 +3,8 @@ github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= diff --git a/cmd/streamdeck/main.go b/cmd/streamdeck/main.go index 5762bd0..38860c3 100644 --- a/cmd/streamdeck/main.go +++ b/cmd/streamdeck/main.go @@ -10,6 +10,7 @@ import ( "github.com/Luzifer/rconfig/v2" "github.com/Luzifer/streamdeck" + "github.com/fsnotify/fsnotify" "github.com/pkg/errors" "github.com/sashko/go-uinput" log "github.com/sirupsen/logrus" @@ -25,9 +26,10 @@ var ( currentBrightness int - userConfig config - activePage page - activeLoops []refreshingDisplayElement + userConfig config + activePage page + activePageName string + activeLoops []refreshingDisplayElement sd *streamdeck.Client @@ -135,6 +137,17 @@ func main() { offTimer = time.NewTimer(userConfig.DisplayOffTime) } + fswatch, err := fsnotify.NewWatcher() + if err != nil { + log.WithError(err).Fatal("Unable to create file watcher") + } + + if userConfig.AutoReload { + if err = fswatch.Add(cfg.Config); err != nil { + log.WithError(err).Error("Unable to watch config, auto-reload will not work") + } + } + for { select { case evt := <-sd.Subscribe(): @@ -158,6 +171,26 @@ func main() { log.WithError(err).Error("Unable to toggle to blank page") } + case evt := <-fswatch.Events: + if evt.Op&fsnotify.Write == fsnotify.Write { + log.Info("Detected change of config, reloading") + + if err := loadConfig(); err != nil { + log.WithError(err).Error("Unable to reload config") + continue + } + + var nextPage = userConfig.DefaultPage + if _, ok := userConfig.Pages[activePageName]; ok { + nextPage = activePageName + } + + if err := togglePage(nextPage); err != nil { + log.WithError(err).Error("Unable to reload page") + continue + } + } + case <-sigs: return @@ -175,6 +208,7 @@ func togglePage(page string) error { activeLoops = nil activePage = userConfig.Pages[page] + activePageName = page sd.ClearAllKeys() for idx, kd := range activePage.Keys {