From 8905acb304150fcfabf99dacb5470023eddb369f Mon Sep 17 00:00:00 2001 From: Nikos Verschore Date: Tue, 5 Oct 2021 11:20:31 +0200 Subject: [PATCH] Add support for automatic reloading the config when it has been changed --- deck.go | 42 ++++++++++++++++++++++++++++++++++++++++-- go.mod | 1 + go.sum | 1 + main.go | 3 ++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/deck.go b/deck.go index a36c767..8963aed 100644 --- a/deck.go +++ b/deck.go @@ -12,6 +12,7 @@ import ( "time" "github.com/atotto/clipboard" + "github.com/fsnotify/fsnotify" "github.com/godbus/dbus" "github.com/muesli/streamdeck" ) @@ -24,11 +25,12 @@ type Deck struct { } // LoadDeck loads a deck configuration. -func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) { - path, err := expandPath(base, deck) +func LoadDeck(dev *streamdeck.Device, base string, deckName string) (*Deck, error) { + path, err := expandPath(base, deckName) if err != nil { return nil, err } + currentDeck = path fmt.Println("Loading deck:", path) dc, err := LoadConfig(path) @@ -70,6 +72,42 @@ func LoadDeck(dev *streamdeck.Device, base string, deck string) (*Deck, error) { d.Widgets = append(d.Widgets, w) } + watcher, err := fsnotify.NewWatcher() + if err == nil { + err = watcher.Add(path) + if err == nil { + + go func() { + for { + select { + case event := <-watcher.Events: + if currentDeck == path { + fmt.Printf("Change: %s: %s\n", event.Op, event.Name) + d, err := LoadDeck(dev, base, deckName) + if err != nil { + fatal(err) + } + err = dev.Clear() + if err != nil { + fatal(err) + } + + deck = d + deck.updateWidgets() + return + } + case error := <-watcher.Errors: + fmt.Printf("Watcher had an error: %s\n", error) + } + } + }() + } else { + fmt.Printf("Failed to watch deck, automatic reloading diabled: %s\n", err) + } + } else { + fmt.Printf("Failed to initialize fsnotify, automatic reloading diabled: %s\n", err) + } + return &d, nil } diff --git a/go.mod b/go.mod index 88c09fe..2257cfd 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/atotto/clipboard v0.1.4 github.com/bendahl/uinput v1.4.1 github.com/flopp/go-findfont v0.1.0 + github.com/fsnotify/fsnotify v1.4.7 github.com/go-ole/go-ole v1.2.4 // indirect github.com/godbus/dbus v4.1.0+incompatible github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 diff --git a/go.sum b/go.sum index 857d8eb..114a30e 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU= github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw= +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/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= diff --git a/main.go b/main.go index addfab1..5e93200 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,8 @@ import ( ) var ( - deck *Deck + deck *Deck + currentDeck string dbusConn *dbus.Conn keyboard uinput.Keyboard