waveterm/pkg/wconfig/filewatcher.go

139 lines
2.8 KiB
Go
Raw Normal View History

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package wconfig
import (
"log"
"path/filepath"
"regexp"
"sync"
"github.com/fsnotify/fsnotify"
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/eventbus"
"github.com/wavetermdev/waveterm/pkg/wavebase"
)
2024-09-11 18:26:43 +02:00
var configDirAbsPath = filepath.Join(wavebase.GetWaveHomeDir(), wavebase.ConfigDir)
var instance *Watcher
var once sync.Once
type Watcher struct {
2024-08-28 03:49:49 +02:00
initialized bool
watcher *fsnotify.Watcher
mutex sync.Mutex
fullConfig FullConfigType
}
type WatcherUpdate struct {
2024-08-28 03:49:49 +02:00
FullConfig FullConfigType `json:"fullconfig"`
}
// GetWatcher returns the singleton instance of the Watcher
func GetWatcher() *Watcher {
once.Do(func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("failed to create file watcher: %v", err)
return
}
instance = &Watcher{watcher: watcher}
2024-08-28 03:49:49 +02:00
err = instance.watcher.Add(configDirAbsPath)
if err != nil {
log.Printf("failed to add path %s to watcher: %v", configDirAbsPath, err)
}
})
return instance
}
func (w *Watcher) Start() {
w.mutex.Lock()
defer w.mutex.Unlock()
log.Printf("starting file watcher\n")
w.initialized = true
w.sendInitialValues()
go func() {
for {
select {
case event, ok := <-w.watcher.Events:
if !ok {
return
}
w.handleEvent(event)
case err, ok := <-w.watcher.Errors:
if !ok {
return
}
log.Println("watcher error:", err)
}
}
}()
}
// for initial values, exit on first error
func (w *Watcher) sendInitialValues() error {
2024-08-28 03:49:49 +02:00
w.fullConfig = ReadFullConfig()
message := WatcherUpdate{
2024-08-28 03:49:49 +02:00
FullConfig: w.fullConfig,
}
w.broadcast(message)
return nil
}
func (w *Watcher) Close() {
w.mutex.Lock()
defer w.mutex.Unlock()
if w.watcher != nil {
w.watcher.Close()
w.watcher = nil
log.Println("file watcher closed")
}
}
func (w *Watcher) broadcast(message WatcherUpdate) {
// send to frontend
eventbus.SendEvent(eventbus.WSEventType{
EventType: eventbus.WSEvent_Config,
Data: message,
})
}
2024-08-28 03:49:49 +02:00
func (w *Watcher) GetFullConfig() FullConfigType {
w.mutex.Lock()
defer w.mutex.Unlock()
2024-08-28 03:49:49 +02:00
return w.fullConfig
}
func (w *Watcher) handleEvent(event fsnotify.Event) {
w.mutex.Lock()
defer w.mutex.Unlock()
fileName := filepath.ToSlash(event.Name)
if event.Op == fsnotify.Chmod {
return
}
if !isValidSubSettingsFileName(fileName) {
return
}
2024-08-28 03:49:49 +02:00
w.handleSettingsFileEvent(event, fileName)
}
var validFileRe = regexp.MustCompile(`^[a-zA-Z0-9_@.-]+\.json$`)
func isValidSubSettingsFileName(fileName string) bool {
if filepath.Ext(fileName) != ".json" {
return false
}
baseName := filepath.Base(fileName)
return validFileRe.MatchString(baseName)
}
func (w *Watcher) handleSettingsFileEvent(event fsnotify.Event, fileName string) {
2024-08-28 03:49:49 +02:00
fullConfig := ReadFullConfig()
w.fullConfig = fullConfig
w.broadcast(WatcherUpdate{FullConfig: w.fullConfig})
}