2015-09-07 13:25:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/BurntSushi/toml"
|
2015-09-12 13:10:03 +00:00
|
|
|
"gopkg.in/fsnotify.v1"
|
2015-09-07 15:39:22 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-09-07 13:25:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileProvider struct {
|
2015-09-12 13:10:03 +00:00
|
|
|
Watch bool
|
2015-09-07 15:39:22 +00:00
|
|
|
Filename string
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 17:32:23 +00:00
|
|
|
func NewFileProvider() *FileProvider {
|
|
|
|
fileProvider := new(FileProvider)
|
|
|
|
// default values
|
|
|
|
fileProvider.Watch = true
|
|
|
|
|
|
|
|
return fileProvider
|
|
|
|
}
|
|
|
|
|
2015-09-12 13:10:03 +00:00
|
|
|
func (provider *FileProvider) Provide(configurationChan chan<- *Configuration) {
|
2015-09-07 13:25:13 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error creating file watcher", err)
|
2015-09-07 15:39:22 +00:00
|
|
|
return
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
2015-09-07 15:39:22 +00:00
|
|
|
file, err := os.Open(provider.Filename)
|
2015-09-07 13:25:13 +00:00
|
|
|
if err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error opening file", err)
|
2015-09-07 15:39:22 +00:00
|
|
|
return
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2015-09-07 15:39:22 +00:00
|
|
|
defer file.Close()
|
2015-09-07 13:25:13 +00:00
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
// Process events
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-watcher.Events:
|
2015-09-12 13:10:03 +00:00
|
|
|
if strings.Contains(event.Name, file.Name()) {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Debug("File event:", event)
|
2015-09-07 22:15:14 +00:00
|
|
|
configuration := provider.LoadFileConfig(file.Name())
|
2015-09-12 13:10:03 +00:00
|
|
|
if configuration != nil {
|
2015-09-07 22:15:14 +00:00
|
|
|
configurationChan <- configuration
|
2015-09-07 21:25:07 +00:00
|
|
|
}
|
2015-09-07 15:39:22 +00:00
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
case error := <-watcher.Errors:
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Watcher event error", error)
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-12 13:10:03 +00:00
|
|
|
if provider.Watch {
|
2015-09-07 16:10:33 +00:00
|
|
|
err = watcher.Add(filepath.Dir(file.Name()))
|
|
|
|
}
|
2015-09-07 15:39:22 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error adding file watcher", err)
|
2015-09-07 15:39:22 +00:00
|
|
|
return
|
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
configuration := provider.LoadFileConfig(file.Name())
|
|
|
|
configurationChan <- configuration
|
2015-09-07 13:25:13 +00:00
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
func (provider *FileProvider) LoadFileConfig(filename string) *Configuration {
|
|
|
|
configuration := new(Configuration)
|
|
|
|
if _, err := toml.DecodeFile(filename, configuration); err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error reading file:", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-07 22:15:14 +00:00
|
|
|
return configuration
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|