尝试优化代码

This commit is contained in:
wyx2685
2024-01-27 01:55:43 +09:00
parent 91e78fbc20
commit f92c5b37d5
4 changed files with 44 additions and 50 deletions

View File

@@ -2,12 +2,12 @@ package conf
import (
"fmt"
"github.com/fsnotify/fsnotify"
"log"
"path"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func()) error {
@@ -34,7 +34,7 @@ func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func())
case filepath.Base(xDnsPath), filepath.Base(sDnsPath):
log.Println("DNS file changed, reloading...")
default:
log.Println("config dir changed, reloading...")
log.Println("config file changed, reloading...")
}
*p = *New()
err := p.LoadFromPath(filePath)
@@ -51,18 +51,18 @@ func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func())
}
}
}()
err = watcher.Add(path.Dir(filePath))
err = watcher.Add(filePath)
if err != nil {
return fmt.Errorf("watch file error: %s", err)
}
if xDnsPath != "" {
err = watcher.Add(path.Dir(xDnsPath))
err = watcher.Add(xDnsPath)
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}
}
if sDnsPath != "" {
err = watcher.Add(path.Dir(sDnsPath))
err = watcher.Add(sDnsPath)
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}