Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions share/settings/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"

"github.com/fsnotify/fsnotify"
"github.com/jpillora/chisel/share/cio"
Expand Down Expand Up @@ -102,20 +106,60 @@ func (u *UserIndex) addWatchEvents() error {
if err != nil {
return err
}
if err := watcher.Add(u.configFile); err != nil {
configPath, err := filepath.Abs(u.configFile)
if err != nil {
return err
}
if err := watcher.Add(filepath.Dir(configPath)); err != nil {
return err
}
go func() {
for e := range watcher.Events {
if e.Op&fsnotify.Write != fsnotify.Write {
continue
}
if err := u.loadUserIndex(); err != nil {
u.Infof("Failed to reload the users configuration: %s", err)
} else {
u.Debugf("Users configuration successfully reloaded from: %s", u.configFile)
var debounceTimer *time.Timer
var debounceMutex sync.Mutex
for {
select {
case e, ok := <-watcher.Events: {
if !ok {
u.Infof("Stop watching the users configuration: fsnotify Events channel closed.")
return
}
eventPath, err := filepath.Abs(e.Name)
if err != nil {
continue
}
if runtime.GOOS == "windows" {
if !strings.EqualFold(eventPath, configPath) {
continue
}
} else if eventPath != configPath {
continue
}
if e.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Rename) == 0 {
continue
}
if debounceTimer != nil {
debounceTimer.Stop()
}
debounceTimer = time.AfterFunc(200*time.Millisecond, func() {
debounceMutex.Lock()
defer debounceMutex.Unlock()
if err := u.loadUserIndex(); err != nil {
u.Infof("Failed to reload the users configuration: %s", err)
} else {
u.Debugf("Users configuration successfully reloaded from: %s", u.configFile)
}
})
}
case err, ok := <-watcher.Errors: {
if !ok {
u.Infof("Stop watching the users configuration: fsnotify Errors channel closed.")
return
}
// just log and continue running with current config
u.Infof("Error while watching the users configuration: %s", err)
}
}
}
}
}()
return nil
}
Expand Down