mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-08 08:10:08 +00:00
50 lines
898 B
Go
50 lines
898 B
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const configChangeCheckInterval = time.Second
|
|
|
|
type configChangeEvent uint8
|
|
|
|
const (
|
|
configChangeEventUnkown configChangeEvent = iota
|
|
configChangeEventNotExist
|
|
configChangeEventModified
|
|
)
|
|
|
|
func watchConfigChanges(filename string, evt chan configChangeEvent) {
|
|
var (
|
|
initialized bool
|
|
size int64
|
|
modTime time.Time
|
|
)
|
|
|
|
for range time.NewTicker(configChangeCheckInterval).C {
|
|
info, err := os.Stat(filename)
|
|
switch {
|
|
case err == nil:
|
|
// Fine
|
|
|
|
case os.IsNotExist(err):
|
|
evt <- configChangeEventNotExist
|
|
continue
|
|
|
|
default:
|
|
log.WithError(err).Error("Failed to get config stat")
|
|
continue
|
|
}
|
|
|
|
if initialized && (info.Size() != size || !info.ModTime().Equal(modTime)) {
|
|
evt <- configChangeEventModified
|
|
}
|
|
|
|
initialized = true
|
|
size = info.Size()
|
|
modTime = info.ModTime()
|
|
}
|
|
}
|