Improve the configWatcher array to allow N files

This simplifies the code in main and hides it in the watcher!
This commit is contained in:
James Shubin
2016-08-10 11:57:01 -04:00
parent 276219a691
commit d429795737
2 changed files with 14 additions and 6 deletions

View File

@@ -44,15 +44,25 @@ func NewConfigWatcher() *ConfigWatcher {
}
// The Add method adds a new file path to watch for events on.
func (obj *ConfigWatcher) Add(file string) {
func (obj *ConfigWatcher) Add(file ...string) {
if len(file) == 0 {
return
}
if len(file) > 1 {
for _, f := range file { // add all the files...
obj.Add(f) // recurse
}
return
}
// otherwise, add the one file passed in...
obj.wg.Add(1)
go func() {
defer obj.wg.Done()
ch := ConfigWatch(file)
ch := ConfigWatch(file[0])
for {
select {
case <-ch:
obj.ch <- file
obj.ch <- file[0]
continue
case <-obj.closechan:
return

View File

@@ -282,9 +282,7 @@ func run(c *cli.Context) error {
configWatcher := NewConfigWatcher()
events := configWatcher.Events()
if !c.Bool("no-watch") {
for _, f := range c.StringSlice("remote") { // add all the files...
configWatcher.Add(f)
}
configWatcher.Add(c.StringSlice("remote")...) // add all the files...
} else {
events = nil // signal that no-watch is true
}