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. // 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) obj.wg.Add(1)
go func() { go func() {
defer obj.wg.Done() defer obj.wg.Done()
ch := ConfigWatch(file) ch := ConfigWatch(file[0])
for { for {
select { select {
case <-ch: case <-ch:
obj.ch <- file obj.ch <- file[0]
continue continue
case <-obj.closechan: case <-obj.closechan:
return return

View File

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