From d429795737467af26876f597c52b9df1b4789289 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 10 Aug 2016 11:57:01 -0400 Subject: [PATCH] Improve the configWatcher array to allow N files This simplifies the code in main and hides it in the watcher! --- configwatch.go | 16 +++++++++++++--- main.go | 4 +--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/configwatch.go b/configwatch.go index a4b8ce3e..d3ce2cb0 100644 --- a/configwatch.go +++ b/configwatch.go @@ -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 diff --git a/main.go b/main.go index 95f5826b..254d4a35 100644 --- a/main.go +++ b/main.go @@ -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 }