Skip to content
Open
Changes from 1 commit
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
34 changes: 28 additions & 6 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,19 +106,37 @@ func (u *UserIndex) addWatchEvents() error {
if err != nil {
return err
}
if err := watcher.Add(u.configFile); err != nil {
var configPath, _ = filepath.Abs(u.configFile)
if err := watcher.Add(filepath.Dir(configPath)); err != nil {
Comment thread
redneck-f25 marked this conversation as resolved.
Outdated
return err
}
go func() {
var debounceTimer *time.Timer
var debounceMutex sync.Mutex
for e := range watcher.Events {
Comment thread
redneck-f25 marked this conversation as resolved.
Outdated
if e.Op&fsnotify.Write != fsnotify.Write {
eventPath, _ := filepath.Abs(e.Name)
if runtime.GOOS == "windows" {
if !strings.EqualFold(eventPath, configPath) {
continue
}
} else if eventPath != configPath {
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)
if e.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Rename) == 0 {
continue
}
if debounceTimer != nil {
debounceTimer.Stop()
}
debounceTimer = time.AfterFunc(200*time.Millisecond, func() {
Comment thread
redneck-f25 marked this conversation as resolved.
Outdated
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)
}
})
}
}()
return nil
Expand Down